如何确定窗口是否在屏幕外?

发布于 2024-10-11 13:03:21 字数 214 浏览 7 评论 0原文

在 Windows XP 及更高版本中,给定窗口句柄 (HWND),我如何判断窗口位置和大小是否使窗口无法挽回地离开屏幕?例如,如果标题栏可供光标使用,则可以将窗口拖回到屏幕上。我需要发现该窗口实际上是否可见或至少对用户可用。我想我还需要知道如何检测和响应分辨率变化以及如何处理多个显示器。这似乎是一件相当大的事情。我使用的是 C++ 和常规 SDK,因此请将您的答案限制在该平台上,而不是调用 C# 或类似平台。

In Windows XP and above, given a window handle (HWND), how can I tell if the window position and size leaves the window irretrievably off screen? For example, if the title bar is available to the cursor, then the window can be dragged back on screen. I need to discover if the window is in fact visible or at least available to the user. I guess I also need to know how to detect and respond to resolution changes and how to deal with multiple monitors. This seems like a fairly big deal. I'm using C++ and the regular SDK, so please limit your answers to that platform rather than invoking C# or similar.

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

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

发布评论

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

评论(3

自在安然 2024-10-18 13:03:21

Windows 使得确定主显示器上用户工作区域的大小(即,未被任务栏遮挡的屏幕区域)变得相对简单。调用 SystemParametersInfo 函数 并为第一个指定 SPI_GETWORKAREA 标志参数(uiAction)。 pvParam 参数应指向 RECT 结构< /a> 将接收虚拟屏幕坐标中的工作区域坐标。

一旦获得了描述工作区域的坐标,将这些坐标与应用程序窗口的当前位置进行比较即可确定它是否位于这些范围内。

支持多个显示器的愿望使事情变得稍微复杂一些。 SystemParametersInfo 的文档建议您需要调用 GetMonitorInfo 函数 来获取除主监视器之外的监视器的工作区域。它填充名为 MONITORINFOEX 包含定义该监视器工作区域的成员 rcWork,再次以虚拟屏幕坐标表示为 RECT 结构。

要正确执行此操作,您需要枚举用户已连接到系统的所有监视器,并使用 GetMonitorInfo 检索每个监视器的工作区域。

在 Internet 上可以找到一些这样的示例:

最后,您提到想要检测分辨率变化。这比您想象的要简单得多。如您所知,如果您进行过 Windows 编程,操作系统与应用程序通信的主要方式是向您的 WindowProc 函数
在这种情况下,您需要注意 WM_DISPLAYCHANGE< /strong>消息,当显示分辨率改变时发送到所有窗口。 wParam 包含新的图像深度(以每像素位数为单位); lParam 的低位字指定屏幕的水平分辨率,lParam 的高位字指定屏幕的垂直分辨率。

Windows makes it relatively simple to determine the size of a user's working area on the primary monitor (i.e., the area of the screen not obscured by the taskbar). Call the SystemParametersInfo function and specify the SPI_GETWORKAREA flag for the first parameter (uiAction). The pvParam parameter should point to a RECT structure that will receive the coordinates of the working area in virtual screen coordinates.

Once you've got the coordinates that describe the working area, it's a simple matter of comparing those to the current position of your application's window to determine if it lies within those bounds.

The desire to support multiple monitors makes things slightly more complicated. The documentation for SystemParametersInfo suggests that you need to call the GetMonitorInfo function instead to get the working area of a monitor other than the primary. It fills in a structure called MONITORINFOEX that contains the member rcWork that defines the working area of that monitor, again expressed in virtual screen coordinates as a RECT structure.

To do this right, you'll need to enumerate all of the monitors a user has connected to the system and retrieve the working area of each using GetMonitorInfo.

There are a few samples of this to be found around the Internet:

  • MSDN has some sample code for Positioning Objects on a Multiple Display Setup.
  • If you're using MFC, here's what looks to be an excellent example of multiple monitor support.
  • Even if you're not using MFC, that article refers the following link which looks be a real gem as far as explaining how multiple monitor supports works in Windows, even if it's a little bit old school. Like it or not, very little of this has changed in later versions of Windows.

Finally, you mentioned wanting to detect resolution changes. This is much simpler than you probably imagined. As you know if you've done any Windows programming, the primary way that the operating system communicates with your application is by sending messages to your WindowProc function.
In this case, you'll want to watch for the WM_DISPLAYCHANGE message, which is sent to all windows when the display resolution has changed. The wParam contains the new image depth in bits per pixel; the low-order word of the lParam specifies the horizontal resolution and the high-order word of the lParam specifies the vertical resolution of the screen.

眼趣 2024-10-18 13:03:21

您可以使用 MonitorFromRect 或 MonitorFromPoint 检查窗口的左上角点或右下角点是否不包含在任何显示监视器(屏幕外)内。

POINT p;
p.x = x;
p.y = y;
HMONITOR hMon = MonitorFromPoint(p, MONITOR_DEFAULTTONULL);
if (hMon == NULL) {
    // point is off screen
}

You can use MonitorFromRect or MonitorFromPoint to check if window's top left point or bottom right point isn't contained within any display monitor (off screen).

POINT p;
p.x = x;
p.y = y;
HMONITOR hMon = MonitorFromPoint(p, MONITOR_DEFAULTTONULL);
if (hMon == NULL) {
    // point is off screen
}
旧街凉风 2024-10-18 13:03:21

可见性检查非常简单。

RECT rtDesktop, rtView;

GetWindowRect( GetDesktopWindow(), &rtDesktop );
GetWindowRect( m_hWnd, &rtView );

HRGN rgn = CreateRectRgn( rtDesktop.left, rtDesktop.top, rtDesktop.right, rtDesktop.bottom );

BOOL viewIsVisible = RectInRegion( rgn, &rtView );

DeleteObject(rgn);

您不必使用 RectInRegion,我用它来缩短代码。

如果你处理WM_SETTINGCHANGE消息,显示、分辨率变化监控也很容易。

http://msdn.microsoft.com/en- us/library/ms725497(v=vs.85).aspx

更新

正如@Cody Gray 指出的,我认为 WM_DISPLAYCHANGE 比 WM_SETTINGCHANGE 更合适。但 MFC 9.0 库使用 WM_SETTINGCHANGE。

Visibility check is really easy.

RECT rtDesktop, rtView;

GetWindowRect( GetDesktopWindow(), &rtDesktop );
GetWindowRect( m_hWnd, &rtView );

HRGN rgn = CreateRectRgn( rtDesktop.left, rtDesktop.top, rtDesktop.right, rtDesktop.bottom );

BOOL viewIsVisible = RectInRegion( rgn, &rtView );

DeleteObject(rgn);

You don't have to use RectInRegion, I used for shorten code.

Display, resolution change monitoring is also easy if you handle WM_SETTINGCHANGE message.

http://msdn.microsoft.com/en-us/library/ms725497(v=vs.85).aspx

UPDATE

As @Cody Gray noted, I think WM_DISPLAYCHANGE is more appropriate than WM_SETTINGCHANGE. But MFC 9.0 library make use of WM_SETTINGCHANGE.

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