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