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
The [Raymond is currently on vacation; this message was pre-recorded.] |
Comments (6)
Comments are closed. |
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.
//
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.
WS_HIDE works.
Maybe IsWindowVisible should have two arguments — one a boolean controlling this behavior. Everyone pays attention to the arguments.
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().
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!
Maybe a window’s DC’s clip rect will be empty if it’s completely obscured or offscreen?