Search code examples
c++windowsvisual-c++styles

Why does EnumWindows return more windows than I expected?


In VC++, I use EnumWindows(...), GetWindow(...), and GetWindowLong(), to get the list of windows and check whether the window is top window (no other window as owner), and whether the window is visible (WS_VISIBLE). However, although my desktop is showing only 5 windows, this EnumWindows is giving me 50 windows, how funny! Any Windows geek here please help me clarify...


Solution

  • The way to list out only windows in taskbar (or similarly in Alt-Tab box) is described by Raymond in this article on MSDN blog:

    Which windows appear in the Alt+Tab list?

    And this is the super function to check whether a window is shown in alt-tab:

    BOOL IsAltTabWindow(HWND hwnd)
    {
        TITLEBARINFO ti;
        HWND hwndTry, hwndWalk = NULL;
    
        if(!IsWindowVisible(hwnd))
            return FALSE;
    
        hwndTry = GetAncestor(hwnd, GA_ROOTOWNER);
        while(hwndTry != hwndWalk) 
        {
            hwndWalk = hwndTry;
            hwndTry = GetLastActivePopup(hwndWalk);
            if(IsWindowVisible(hwndTry)) 
                break;
        }
        if(hwndWalk != hwnd)
            return FALSE;
    
        // the following removes some task tray programs and "Program Manager"
        ti.cbSize = sizeof(ti);
        GetTitleBarInfo(hwnd, &ti);
        if(ti.rgstate[0] & STATE_SYSTEM_INVISIBLE)
            return FALSE;
    
        // Tool windows should not be displayed either, these do not appear in the
        // task bar.
        if(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW)
            return FALSE;
    
        return TRUE;
    }
    

    Credited to the source code here:
    http://www.dfcd.net/projects/switcher/switcher.c