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

4
这里是问题:
我有一个应用程序(C#.NET,Windows 7),在退出时记住主窗体的位置。加载时检索并应用设置。我遇到了使用双显示器设置的用户的问题。
我们大多使用带有扩展坞和副屏幕的HP笔记本电脑。用户有时必须拆下他们的笔记本电脑。当用户在第二个监视器上运行应用程序,然后关闭它,拆下笔记本电脑并重新启动应用程序时 - 它就会越界(因为应用程序记住了位置)。
我需要一种方法来查看第二个监视器是否连接。
这是我已经尝试过的内容:
System.Windows.Forms.Screen.AllScreens - 即使笔记本电脑未插入,此数组也有两个监视器(我认为这是因为第二个监视器仍然出现在“控制面板”->“显示”中)
System.Windows.Forms.SystemInformation.MonitorCount - 此属性也适用于相同的情况。
谢谢。
谢谢大家,但在这种情况下,我们笔记本电脑的问题如下:
我们使用2x客户端软件在笔记本电脑上访问在服务器上运行的应用程序。 2x本身具有兼容性选项卡中的禁用桌面组合设置。如果选中此设置,则第二个监视器始终可用(即使笔记本电脑未插入)。
因此,解决方法是打开此设置。
再次感谢。

我在这里详细回答了这个问题,但考虑到即使没有连接额外的显示器,你的笔记本电脑也会认为它们已经连接了一个,所以我不确定它是否有效。链接的答案要么有解决方案,要么这个问题是无法解决的。 - Cody Gray
谢谢Cody,但这并没有解决问题。就像你提到的那样,我的笔记本电脑在从站点拆卸后仍然“认为”它们有两个显示器。我以为可能有一个属性可以检查“活动”监视器,但我猜只能自求多福了。 - myroslav
第二个显示器在控制面板-> 显示器中是如何“显示”的?Windows是否认为它正在使用它? 第二个显示器在控制面板->显示器中是如何“显示”的?Windows是否认为它正在使用它? - Cody Gray
我在控制面板->显示中看到了两个活动监视器。很抱歉,由于我是新用户,无法发布图片。谢谢。 - myroslav
1个回答

3

尝试这个方法...如果情况像你所描述的那样糟糕(在控制面板中看到监视器等),它可能无法帮助,但还是值得一试。将以下方法添加到您的项目中:

  /// <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。如果返回false,则将其放在主屏幕上。我使用这个方法来使用笔记本电脑在坞站上运行程序,效果非常好。但是,如果您的PC在没有存在额外显示器的情况下报告了额外的显示器,那么我不知道结果会如何。如果真的是这样,我怀疑这是坞站(或Windows...)的问题,您可能无法通过代码解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接