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

发布于 2024-11-26 09:45:43 字数 658 浏览 0 评论 0原文

问题是:

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

我们主要使用带有扩展坞和辅助显示器的惠普笔记本电脑。用户有时必须拔出笔记本电脑。当用户在辅助显示器上运行应用程序,然后将其关闭,断开笔记本电脑的连接并重新启动该应用程序时 - 这是越界的(因为应用程序会记住该位置)。

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


这是我已经尝试过的:

System.Windows.Forms.Screen.AllScreens - 即使笔记本电脑未连接,该阵列也有两个显示器(我认为这是因为第二个显示器仍然显示在控制面板中 ->显示)

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

谢谢。


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

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

因此解决方法是打开此设置。

再次感谢

Here is the problem:

I have an application (C#.NET, Windows 7) that remembers the location of the main form on exit. On load the settings are retrieved and applied. I have a problem with the users that run dual monitors setup.

We mostly use HP laptops with docking stations and secondary monitors. Users sometimes have to undock their laptops. When the user has an application running on a secondary monitor, then shuts it down, undocks the laptop and restarts the application - it is off bounds (since the location is remembered by application).

I need a way to see if the second monitor is connected.


Here is what I have already tried:

System.Windows.Forms.Screen.AllScreens - this array has two monitors even if the laptop is undocked (I assume its due to the fact that the second monitor still shows up in Control Panel -> Display)

System.Windows.Forms.SystemInformation.MonitorCount - the same applies to this property.

Thank you.


Thank you guys, but the problem with our laptops in this case was the following:

We are using 2x client software on our laptops to access the applications that run on the server. 2x itself has a setting Disable Desktop Composition in Compatibility tab. If this is checked off the second monitor always appears to be available (even when the laptop is undocked).

So the fix was to turn this setting on.

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-12-03 09:45:43

试试这个...如果事情像您所描述的那样糟糕(在控制面板中查看显示器等),它可能没有帮助,但值得一试。将以下方法添加到您的项目中:

  /// <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...)的问题,并且您可能没有通过代码解决此问题的好方法。

Try this... It may not help if things are as bad as you are describing (seeing the monitor in Control Panel and all), but it's worth a shot. Add the following method to your project:

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

Then when you load the location of your form, pass the point to which you intend to load it and the size of your form. The method will return true if the form will be visible enough that the user could move it, and false otherwise. If it's false, just put it on the main screen. I use this for my program using a laptop on a docking station with flawless results -- but again if your PC's are somehow reporting extra monitors when they don't exist, I don't know what the results will be. If that's really true, I suspect it's a problem with the docking station (or Windows...), and you might not have a good way around this through code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文