Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a wpf app and I was displaying multiple windows into multiple monitors.

C#
/// <summary>
      /// Display the panels received as a parameter over the screen fixe by static coordinates
      /// </summary>
      /// <param name="panel"></param>
      public static void ShowByCoordinates(Window window, int screenId, int LeftTransform, int TopTransform)
      {
          System.Drawing.Rectangle bounds = System.Drawing.Rectangle.Empty;
          System.Windows.Forms.Screen s0 = System.Windows.Forms.Screen.AllScreens[screenId];
          bounds = s0.WorkingArea;
          window.Left = bounds.X + LeftTransform;
          window.Top = bounds.Y + TopTransform;
          window.Show();

      }




that screen id parameter was get it from app.config file. Problem is those monitors/tvs change their ids if the pc is reset. I could not find a good way to get fixed identifiers from the monitors and convert them into screens where my windows can be displayed. I have look how to get monitors with

C#
<pre lang="C#">
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DesktopMonitor");Console.WriteLine(&quot;PNP Device ID: {0}&quot;, obj[&quot;PNPDeviceID&quot;]);


but I dont know how to convert them into screen or what property use to point them always.Any help would be very appreciate it.
Posted

1 solution

What you're calling "screen ID" is just the index of the screen within the array. You can't rely on it to identify the screen.

You'd probably have better luck using the DeviceName property[^]:
C#
public static void ShowByCoordinates(Window window, string screenName, int LeftTransform, int TopTransform)
{
    Screen s0 = Screen.AllScreens.FirstOrDefault(s => s.DeviceName == screenName) ?? Sreen.PrimaryScreen;
    Rectangle bounds = s0.WorkingArea;
    window.Left = bounds.X + LeftTransform;
    window.Top = bounds.Y + TopTransform;
    window.Show();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900