How a Multi Monitor application can detect a missi

2020-07-30 01:11发布

问题:

An application with docking capabilities can save the desktop with the positions of all windows including the ones on separate monitors.

If the saved desktop is reloaded but one or more of the monitors is not connected, the application should detect this. I have the following:

    ...
    Windows windows = Window.GetWindow(pane);
    if (window != null)
    {
      PaneTookWindow = toolWindow = window.Content as PaneToolWindow;
      if (toolWindow != null)
      {
        if (!AreaInScreenBounds(new Rect(toolWindow.Left, toolWindow.Top, toolWindow.Width, toolWindow.Height)))
        {
           pane.ExecuteCommand(ContentPaneCommands.ChangeToDocument);
        }
      }
    }
    ...


   private static bool AreaInScreenBounds(Rect area)
   {
      if (Application.Current != null && Application.Current.MainWindow != null)
      {
         Rect [] screeAreas = Application.Current.MainWindow.GetScreenAreas();
         return screenAreas.Any(screen => screen.Contains(area));
      }
      return false;
   }

The problem is that this method does not detect whether the monitor is no longer available, but whether the area is outside of the MainWindow area.

Does anyone know, how to detect a disconnected monitor or a no longer available area?

回答1:

Screen Class represents a display device or multiple display devices on a single system. You should want the 'Bounds' attribute for the resolutions and the 'AllScreens' attribute for the number of displays connected

int index;
int upperBound; 
Screen [] screens = Screen.AllScreens;
upperBound = screens.GetUpperBound(0);
for(index = 0; index <= upperBound; index++)
{
// For each screen, add the screen properties to a list box.
   listBox1.Items.Add("Device Name: " + screens[index].DeviceName);
   listBox1.Items.Add("Bounds: " + screens[index].Bounds.ToString());
   listBox1.Items.Add("Type: " + screens[index].GetType().ToString());
   listBox1.Items.Add("Working Area: " + screens[index].WorkingArea.ToString());
   listBox1.Items.Add("Primary Screen: " + screens[index].Primary.ToString());
}

More info here: http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx