Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 904
hatcat Avatar asked Jan 13 '11 15:01

hatcat


2 Answers

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
}
like image 182
Evgeny Avatar answered Oct 22 '22 02:10

Evgeny


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.

like image 26
9dan Avatar answered Oct 22 '22 01:10

9dan