Thread: How to find open windows ?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    How to find open windows ?

    Quite simple, huh ? I can't seem to find an answer though. I am currently using EnumWindows() but it lists every window (including child windows) and this is not what I want to do. I want to find all windows that are on the taskbar (or alt-tab or whatever). How would I do that ? Google isn't my friend on this topic I think.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by MSDN
    The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style.
    Quote Originally Posted by MSDN
    The GetWindowInfo function retrieves information about the specified window.
    Quote Originally Posted by MSDN
    The WINDOWINFO structure contains window information.
    30 seconds...

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I want to find all windows that are on the taskbar (or alt-tab or whatever). How would I do that ?
    Windows in the Alt-Tab list

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by abachler View Post
    30 seconds...
    Tried that. It excludes windows which should be on that list and includes stuff that shouldn't be there (including start menu, SysFader, CiceroUIWndFrame (wtf is this ?)...).

    Code:
    BOOL CALLBACK FillWindowVec(HWND wnd, LPARAM)
    {
        WINDOWINFO wi;
        GetWindowInfo(wnd, &wi);
        if(!(wi.dwStyle & WS_CHILD)) WindowVec.push_back(wnd);
    }
    I will try Bob's method.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Using the code listed in Bob's link WindowVec now gets filled with only a single element whose caption is "0 - ".

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by MSDN
    The GetWindow function retrieves a handle to a window that has the specified relationship (Z-Order or owner) to the specified window.
    start with HWND 0 (desktop) and step through all windows.

  7. #7
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Chen did say it was pseudo-code, but this produces the titles of the windows in the Alt-Tab list for me.
    Code:
    #include <windows.h>
    #include <iostream>
    
    BOOL IsAltTabWindow(HWND hwnd)
    {
        LONG_PTR exStyles = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
        // Start at the root owner
        HWND hwndWalk = hwnd;
        // appwindows treated as no owner, so don't try and find one
        if(!(exStyles & WS_EX_APPWINDOW))
        {
            hwndWalk = GetAncestor(hwnd, GA_ROOTOWNER);
        }
    
        // See if we are the last active visible popup
        HWND hwndTry = hwndWalk;
        while ((hwndTry = GetLastActivePopup(hwndWalk)) != hwndTry) {
            if (IsWindowVisible(hwndTry)) break;
            hwndWalk = hwndTry;
        }
        // tool windows are treated as not visible so they'll never appear in the list 
        // fail them here
        return (hwndWalk == hwnd) && !(exStyles & WS_EX_TOOLWINDOW);
    }
    
    BOOL CALLBACK Enum(HWND hwnd, LPARAM lParam)
    {
        if(IsAltTabWindow(hwnd) && IsWindowVisible(hwnd))
        {
            WCHAR buf[260];
            GetWindowText(hwnd, buf, ARRAYSIZE(buf));
            std::wcout << L"Alt-tab window with title " << buf << L" found\n";
        }
        return TRUE;
    }
    
    int __cdecl main()
    {
        EnumWindows(&Enum, 0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open windows file
    By woofer in forum Windows Programming
    Replies: 2
    Last Post: 11-25-2008, 09:37 PM
  2. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 03:36 PM
  3. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  4. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM