如何确定窗口是否在屏幕外?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 theSPI_GETWORKAREA
flag for the first parameter (uiAction
). ThepvParam
parameter should point to aRECT
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 theGetMonitorInfo
function instead to get the working area of a monitor other than the primary. It fills in a structure calledMONITORINFOEX
that contains the memberrcWork
that defines the working area of that monitor, again expressed in virtual screen coordinates as aRECT
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:
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. ThewParam
contains the new image depth in bits per pixel; the low-order word of thelParam
specifies the horizontal resolution and the high-order word of thelParam
specifies the vertical resolution of the screen.您可以使用 MonitorFromRect 或 MonitorFromPoint 检查窗口的左上角点或右下角点是否不包含在任何显示监视器(屏幕外)内。
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).
可见性检查非常简单。
您不必使用 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.
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.