检查第二台显示器是否在.NET中连接(双显示器设置,带扩展坞的笔记本电脑)

时间:2011-07-22 17:11:56

标签: c# .net windows multiple-monitors

问题在于:

我有一个应用程序( C#.NET,Windows 7 ),它会在退出时记住主窗体的位置。加载时,将检索并应用设置。我遇到运行双显示器设置的用户的问题。

我们主要使用带有扩展坞和辅助显示器的惠普笔记本电脑。用户有时不得不取消他们的笔记本电脑。当用户在辅助监视器上运行应用程序时,将其关闭,取消停靠笔记本电脑并重新启动应用程序 - 它已关闭(因为该位置被应用程序记住)。

我需要一种方法来查看第二台显示器是否已连接。


这是我已经尝试过的:

System.Windows.Forms.Screen.AllScreens - 即使笔记本电脑未对接,这个阵列也有两个显示器(我假设它是由于第二个显示器仍显示在控制面板中 - >显示)

System.Windows.Forms.SystemInformation.MonitorCount - 同样适用于此属性。

谢谢。


谢谢大家,但在这种情况下我们的笔记本电脑的问题如下:

我们在笔记本电脑上使用2x客户端软件来访问服务器上运行的应用程序。 2x本身在兼容性选项卡中有一个设置禁用桌面组合。如果已选中此选项,则第二台显示器似乎始终可用(即使笔记本电脑未对接)。

所以解决方法是打开此设置。

再次感谢

1 个答案:

答案 0 :(得分:2)

试试这个......如果事情和你描述的一样糟糕(在控制面板和所有视频中看到显示器),它可能无济于事,但它值得一试。将以下方法添加到项目中:

  /// <summary>
  /// Returns whether at least the titlebar of a form would be on a viewable portion of the screen
  /// </summary>
  /// <param name="FormLocation">The location of the form</param>
  /// <param name="FormSize">The size of the form</param>
  /// <returns></returns>
  protected bool FormWouldBeVisible(Point FormLocation, Size FormSize)
  {
     //The FromPoint method returns the screen OR CLOSEST SCREEN to the point you give...
     Screen theScreen = Screen.FromPoint(FormLocation);
     int titleBar = SystemInformation.CaptionHeight;
     //Test if enough of the title bar will be visible so that the user can move the form if desired...
     if ((theScreen.Bounds.Bottom >= (FormLocation.Y + titleBar)) && //If the bottom of the screen is below the title bar
           (theScreen.Bounds.Top <= FormLocation.Y) && //If the top of the screen is above the top of the title bar
           (theScreen.Bounds.Left <= (FormLocation.X + FormSize.Width - titleBar)) && //If the left of the screen is left of a little bit of the title bar
           (theScreen.Bounds.Right >= (FormLocation.X + titleBar))) //If the right of the screen is right of a little bit of the title bar
     {
        //The form is moveable
        return true;
     }
     //The point at which the form is to be loaded is not on a visible part of any screen
     else return false;
  }

然后,当您加载表单的位置时,传递要加载它的点和表单的大小。如果表单足够可见,用户可以移动它,则该方法将返回true,否则返回false。如果它是假的,只需将它放在主屏幕上即可。我使用笔记本电脑在扩展坞上使用这个程序可以获得完美无瑕的结果 - 但是如果你的电脑在不存在时以某种方式报告额外的显示器,我不知道结果会是什么。如果这是真的,我怀疑这是扩展坞(或Windows ...)的一个问题,你可能无法通过代码解决这个问题。

相关问题