如何确定窗口是否在屏幕外? [英] How do I determine if a window is off-screen?

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

问题描述

在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.

推荐答案

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

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.



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


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.

要做到这一点,您需要枚举用户已经连接到系统的所有监视器并检索工作区每个都使用 GetMonitorInfo

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有一些示例代码在多显示设置上定位对象

  • 如果您使用的是MFC,下面是多显示器支持的出色示例

  • 即使您不使用MFC,该文章引用以下链接,它看起来像是真正的宝石解释多个显示器如何支持在Windows中工作,即使它是一个有点旧的学校。不管喜欢与否,在以后的Windows版本中,这一点都有所改变。

  • 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.



最后,提到想要检测分辨率变化。这比你想象的要简单得多。如果您已经完成了任何Windows编程,操作系统与您的应用程序通信的主要方式是向您的 WindowProc 函数

在这种情况下,您需要注意 WM_DISPLAYCHANGE 消息到所有窗口时,显示分辨率已更改。 wParam 包含以像素为单位的新图像深度; lParam 的低位字指定水平分辨率, lParam 的高位字指定垂直屏幕分辨率。


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.

这篇关于如何确定窗口是否在屏幕外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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