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.
Share
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
SystemParametersInfofunction and specify theSPI_GETWORKAREAflag for the first parameter (uiAction). ThepvParamparameter should point to aRECTstructure 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
SystemParametersInfosuggests that you need to call theGetMonitorInfofunction instead to get the working area of a monitor other than the primary. It fills in a structure calledMONITORINFOEXthat contains the memberrcWorkthat defines the working area of that monitor, again expressed in virtual screen coordinates as aRECTstructure.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
WindowProcfunction.In this case, you’ll want to watch for the
WM_DISPLAYCHANGEmessage, which is sent to all windows when the display resolution has changed. ThewParamcontains the new image depth in bits per pixel; the low-order word of thelParamspecifies the horizontal resolution and the high-order word of thelParamspecifies the vertical resolution of the screen.