开发者

How to check if my window gets hidden/visible?

If i press the "show desktop" button in Windows7, my program will still think its not minimized, and if i press WIN+D while my program is focused, only then my program will catch this minimize command. How can i check for 100% sure that my program is visible or not?

Here is my main loop:

while(!done){
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
        if(msg.message == WM_QUIT){
            done = TRUE;
        }else{
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }else if(active){
        render();
    }
}

Edit3: Is this good? looks like its working:

case WM_WINDOWPOSCHANGED:
{
        flags = ((PWINDOWPOS)lParam)->flags;
        if((flags & 0x8000) && (flags & SWP_NOCOPYBITS) && (flags & SWP_FRAMECHANGED)){
            active = !(flags & SWP_NOACTIVATE);
        }
        if((flags & 0x1000) && (flags & 0x0800) && (flags & SWP_NOMOVE) && (flags & SWP_NOSIZE)){
            active = 1;
        }
}
case WM_ACTIVATE:
{
开发者_StackOverflow中文版    active = !HIWORD(wParam);
    return 0;
}


WM_ACTIVATE is sent when another window becomes active. When you say show desktop, no other window becomes active, so technically, your app is still active, even though it has been minimized.

You probably want to watch for WM_WINDOWPOSCHANGED. You can look at the flags to see what type of position event it was, or you can check IsIconic and IsWindowVisible whenever the window position changes.


There are a variety of potential functions that may get you the information you need depending on exactly what you want to do:

  • GetForegroundWindow() : Gets the window the user is currently "working" with. You could use this if you only wanted to draw things when a user is using your application but not another one.
  • GetActiveWindow() : Returns the active window within the calling thread which is probably not what you want. This might be useful if you wish to enable/disable drawing depending on which window was active within your own application.
  • GetFocus() : Returns the window with the current keyboard focus in the calling thread. Probably not what you want and use GetForegorundWindow() instead.
  • IsWindowVisible(): Returns whether the show/hide flag of the window is set to visible. This doesn't actually tell you whether the window is actually visible on the screen.
  • GetTopWindow(): Tells you the highest window in the z-order but not whether it actually has the focus/foreground. It may be possible for your window to be focused/activate/foreground but not have the highest z-order (I think anyways).

From your comments, however, you seem to actually want to see if there is at least one pixel of your window actually visible on the screen. For that I would probably use the technique mentioned in this SO question using the strangely named GetRandomRgn() although a simpler check may be to use GetClipBox() and check the return code for NULLREGION.


With Windows 8/10, there's another window visibility flag which is separate from IsWindowVisible. Check DwmGetWindowAttribute and the DWMWA_CLOAKED attribute.

Additionally, windows can be semitransparent, and GetLayeredWindowAttributes can tell you what the alpha level of a window is.


IsWindowVisible tells you if your window is visible. GetTopWindow tells you if it is the uppermost one in the Z order.


Try WM_ACTIVATEAPP.

wParam will be false if a window from any other app is getting focus. This includes pressing the "show desktop" button.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜