When is a window visible yet not visible?

Date:May 20, 2005 / year-entry #124
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050520-18/?p=35593
Comments:    6
Summary:Today, a quick puzzler. Consider the following code fragment: ShowWindow(hwnd, SW_SHOWNORMAL); assert(IsWindowVisible(hwnd)); We just showed the window, certainly it is visible, right? Yet the assertion can fire (even in the absence of multi-threading). Why? Answer below - stop reading if you want to try to solve it yourself. Take a look at the IsWindowVisible function....

Today, a quick puzzler.

Consider the following code fragment:

 ShowWindow(hwnd, SW_SHOWNORMAL);
 assert(IsWindowVisible(hwnd));

We just showed the window, certainly it is visible, right? Yet the assertion can fire (even in the absence of multi-threading). Why?

Answer below - stop reading if you want to try to solve it yourself.

Take a look at the IsWindowVisible function.

If the specified window, its parent window, its parent's parent window, and so forth, have the WS_VISIBLE style, the return value is nonzero. Otherwise, the return value is zero.

The WS_VISIBLE style indicates that this window is visible in its parent. But the parent itself might not be visible, in which case IsWindowVisible returns FALSE.

[Raymond is currently on vacation; this message was pre-recorded.]


Comments (6)
  1. Chango V. says:

    Yeah, I was trapped by this once. The design makes sense, but the function name is definitely misleading. And it’s so easy to use (you’d think) that you’ll never want to read its documentation.

    //

  2. Spire says:

    It’s not the name of the IsWindowVisible() function that’s misleading; if anything, it’s the name of the WS_VISIBLE style bit.

    Maybe it should’ve been called WS_NOTEXPLICITLYHIDDEN, or something equally cumbersome. Then again, maybe not.

    A small improvement to that would be to reverse the logic of the flag and call it WS_EXPLICITLYHIDDEN.

  3. cola says:

    WS_HIDE works.

    Maybe IsWindowVisible should have two arguments — one a boolean controlling this behavior. Everyone pays attention to the arguments.

  4. Spire says:

    Calling it WS_HIDE would have the same problem as calling it WS_VISIBLE: Not having the WS_HIDE bit set might imply to some people that the window is visible, when it actually might be hidden due to its parent having WS_HIDE set.

    Adding a second parameter to IsWindowVisible() is a much better idea. While we’re at it, we might as well add the ability to determine if a window is completely obscured by other windows, or offscreen, or visible on a specific monitor. Hey, sounds like a job for IsWindowVisibleEx().

  5. kalleboo says:

    Alternative answer: When it’s offscreen, or overlapped by another window. Sure, it’s visible to according to the OS, but the user wouldn’t agree!

  6. Neil says:

    Maybe a window’s DC’s clip rect will be empty if it’s completely obscured or offscreen?

Comments are closed.


*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