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

前端 未结 3 2060
予麋鹿
予麋鹿 2021-01-04 16:48

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

3条回答
  •  走了就别回头了
    2021-01-04 16:51

    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.

提交回复
热议问题