Multi Monitor应用程序如何检测丢失的监视器

时间:2012-03-12 16:02:32

标签: c# wpf multiple-monitors

具有对接功能的应用程序可以使用所有窗口的位置保存桌面,包括单独监视器上的窗口。

如果重新加载已保存的桌面但未连接一个或多个监视器,则应用程序应检测到此情况。 我有以下内容:

    ...
    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;
   }

问题是此方法无法检测监视器是否不再可用,但该区域是否在MainWindow区域之外。

有谁知道,如何检测断开连接的显示器或不再可用的区域?

1 个答案:

答案 0 :(得分:1)

Screen Class表示单个系统上的显示设备或多个显示设备。你应该想要分辨率的'Bounds'属性和连接的显示数量的'AllScreens'属性

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());
}

此处有更多信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx