Determining whether your window is covered

Date:September 2, 2003 / year-entry #43
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20030902-00/?p=42693
Comments:    0
Summary:The method described in the previous coding blog entry works great if you are using the window visibility state to control painting, since you're using the paint system itself to do the heavy lifting for you. To obtain this information outside of the paint loop, use GetDC and GetClipBox. The HDC that comes out of...

The method described in the previous coding blog entry works great if you are using the window visibility state to control painting, since you're using the paint system itself to do the heavy lifting for you.

To obtain this information outside of the paint loop, use GetDC and GetClipBox. The HDC that comes out of GetDC is clipped to the visible region, and then you can use GetClipBox to extract information out of it.

Start with our scratch program and add these lines:

void CALLBACK
PollTimer(HWND hwnd, UINT uMsg, UINT_PTR idTimer, DWORD dwTime)
{
    HDC hdc = GetDC(hwnd);
    if (hdc) {
        RECT rcClip, rcClient;
        LPCTSTR pszMsg;
        switch (GetClipBox(hdc, &rcClip)) {
        case NULLREGION:
            pszMsg = TEXT("completely covered"); break;
        case SIMPLEREGION:
            GetClientRect(hwnd, &rcClient);
            if (EqualRect(&rcClient, &rcClip)) {
                pszMsg = TEXT("completely uncovered");
            } else {
                pszMsg = TEXT("partially covered");
            }
            break;
        case COMPLEXREGION:
            pszMsg = TEXT("partially covered"); break;
        default:
            pszMsg = TEXT("Error"); break;
        }
        // If we wanted, we could also use RectVisible
        // or PtVisible - or go totally overboard by
        // using GetClipRgn
        ReleaseDC(hwnd, hdc);

        SetWindowText(hwnd, pszMsg);
    }
}

BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
    SetTimer(hwnd, 1, 1000, PollTimer);
    return TRUE;
}

Once a second, the window title will update with the current visibility of the client rectangle.

Polling is more expensive than letting the paint system do the work for you, so do try to use the painting method first.

(Side note: The reason why part 9 of the scrollbar series is so slow to come out finally struck me as I tried to write it: It's too big. I've split it into parts 9 through 12, with an optional part 13; that may make the little pieces more manageable. Part 9 is written, but I want to hold off posting it until I've got at least through part 12, because something from the later parts may force me to rewrite part 9. A somewhat self-absorbed and rather boring insight into the writing process.)



*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index