如何检查窗口在用户屏幕上是否完全可见? [英] How can I check that a window is fully visible on the user's screen?

查看:52
本文介绍了如何检查窗口在用户屏幕上是否完全可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查 WinForm 是否在屏幕上完全可见(例如,没有超出屏幕范围?)

Is there a way to check that a WinForm is fully visible on the screen (eg is not out of bounds of the screen?)

我已经尝试为此使用 SystemInformation.VirtualScreen,只要虚拟屏幕是矩形,它就可以很好地工作,但是一旦它不是(例如 L 形的 3 个屏幕),SystemInformation.VirtualScreen 就会返回最小的矩形包含所有可见像素(因此 L 右上角的窗口将不可见,尽管它在虚拟屏幕中)

I've tried using SystemInformation.VirtualScreen for this, which works great as long as the virtual screen is a rectangle, but as soon as it's not (eg 3 screens in a L shape), SystemInformation.VirtualScreen returns the smallest rectangle containing all the visible pixels (so a window on the upper right corner of the L won't be visible although it's in the virtual screen)

我试图实现这一目标的原因是我希望我的程序在它们所在的最后一个位置打开它的子​​窗口,但如果用户更改,我不希望这些窗口消失正在设置(例如从他的笔记本电脑上拔下额外的屏幕)

The reason I'm trying to achieve this is that I'd like my program to open its child windows in the last location they were on, but I don't want those window to be out of view if the user changes is setup (eg unplugs the extra screen from his laptop)

推荐答案

这是我最终做到的:

bool isPointVisibleOnAScreen(Point p)
{
    foreach (Screen s in Screen.AllScreens)
    {
        if (p.X < s.Bounds.Right && p.X > s.Bounds.Left && p.Y > s.Bounds.Top && p.Y < s.Bounds.Bottom)
            return true;
    }
    return false;
}

bool isFormFullyVisible(Form f)
{
    return isPointVisibleOnAScreen(new Point(f.Left, f.Top)) && isPointVisibleOnAScreen(new Point(f.Right, f.Top)) && isPointVisibleOnAScreen(new Point(f.Left, f.Bottom)) && isPointVisibleOnAScreen(new Point(f.Right, f.Bottom));
 }

如果用户在他的显示设置中有一个漏洞"(见下面的例子),可能会有一些误报(见下面的例子),但我认为我的任何用户都不会遇到这种情况:)

There might be some false positive if the user has a "hole" in his display setup (see example below) but I don't think any of my users will ever be in such a situation :)

   [1]
[2][X][3]

这篇关于如何检查窗口在用户屏幕上是否完全可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆