How do I determine if a window is off-screen? How do I determine if a window is off-screen? windows windows

How do I determine if a window is off-screen?


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.


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}


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.