如何判断我的表单是否显示在屏幕之外?

时间:2022-04-29 21:13:46

So I have two forms, mainform and extraform.
extraform is set always moved to the right of mainform when mainform initializes
Sometimes mainform takes up both monitors and extraform is pushed off the screen never to be seen again. I would like to prevent this if possible. How can I do so? It must support dual monitors, that may or may not have distance between them (i.e. screen 1 is 20px to the left of screen 2).

所以我有两种形式,mainform和extraform。当mainform初始化时,extraform设置总是移动到mainform的右侧有时mainform占用两个监视器并且extraform被推离屏幕永远不会再被看到。我想尽可能防止这种情况发生。我怎么能这样做?它必须支持双监视器,它们之间可能有或没有距离(即屏幕1是屏幕2左侧20px)。

How can I do this?

我怎样才能做到这一点?

3 个解决方案

#1


You can use the Screen class to work out where your window is relative to the desktop. The Screen class has a FromRectangle method, so you can figure out which screen you should be positioning your Form on (by passing your form's Bounds property in).

您可以使用Screen类来确定窗口相对于桌面的位置。 Screen类有一个FromRectangle方法,因此你可以找出你应该在哪个屏幕上定位你的Form(通过传递你的表单的Bounds属性)。

Each Screen object has a Bounds property, which you can use to compare to the location and size of your window, and adjust them accordingly.

每个Screen对象都有一个Bounds属性,您可以使用该属性来比较窗口的位置和大小,并相应地调整它们。

#2


It depends what you want should happen when extraform is pushed beyond the bounds of the screen(s).

这取决于当推出extraform超出屏幕范围时你想要发生什么。

However, to find out whether or not it's being pushed off, it's quite simple using the System.Windows.Forms.Screens class. Then you can do bounds checking like so:

但是,要了解它是否被推迟,使用System.Windows.Forms.Screens类非常简单。然后你可以像这样做边界检查:

        foreach (var screen in Screen.AllScreens)
        {
            if(screen.Bounds.Contains(this.Bounds))
            {
                Console.WriteLine("Device "+screen.DeviceName+" contains form!");
            }
        }

Code assumes being in a form. Note that this code only prints that a screen contains the form if the form is completely contained on the screen. But this should be rather simple to fix, depending on your needs.

代码假定采用某种形式。请注意,如果表单完全包含在屏幕上,此代码仅打印屏幕包含表单。但根据您的需要,这应该很容易修复。

#3


Perhaps the DesktopLocation property in your Forms can give you a clue about what's happening with what's happening with them

也许您表单中的DesktopLocation属性可以为您提供有关正在发生的事情的线索

#1


You can use the Screen class to work out where your window is relative to the desktop. The Screen class has a FromRectangle method, so you can figure out which screen you should be positioning your Form on (by passing your form's Bounds property in).

您可以使用Screen类来确定窗口相对于桌面的位置。 Screen类有一个FromRectangle方法,因此你可以找出你应该在哪个屏幕上定位你的Form(通过传递你的表单的Bounds属性)。

Each Screen object has a Bounds property, which you can use to compare to the location and size of your window, and adjust them accordingly.

每个Screen对象都有一个Bounds属性,您可以使用该属性来比较窗口的位置和大小,并相应地调整它们。

#2


It depends what you want should happen when extraform is pushed beyond the bounds of the screen(s).

这取决于当推出extraform超出屏幕范围时你想要发生什么。

However, to find out whether or not it's being pushed off, it's quite simple using the System.Windows.Forms.Screens class. Then you can do bounds checking like so:

但是,要了解它是否被推迟,使用System.Windows.Forms.Screens类非常简单。然后你可以像这样做边界检查:

        foreach (var screen in Screen.AllScreens)
        {
            if(screen.Bounds.Contains(this.Bounds))
            {
                Console.WriteLine("Device "+screen.DeviceName+" contains form!");
            }
        }

Code assumes being in a form. Note that this code only prints that a screen contains the form if the form is completely contained on the screen. But this should be rather simple to fix, depending on your needs.

代码假定采用某种形式。请注意,如果表单完全包含在屏幕上,此代码仅打印屏幕包含表单。但根据您的需要,这应该很容易修复。

#3


Perhaps the DesktopLocation property in your Forms can give you a clue about what's happening with what's happening with them

也许您表单中的DesktopLocation属性可以为您提供有关正在发生的事情的线索