How a Multi Monitor application can detect a missing monitor

孤街浪徒 提交于 2019-12-06 12:30:01

问题


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



来源:https://stackoverflow.com/questions/9670507/how-a-multi-monitor-application-can-detect-a-missing-monitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!