Get all windows from Alt + Tab excluding Metro windows - Windows 8/8.1

912 views Asked by At

Is it possible to get all windows hwnd from a Alt + Tab window - excluding Metro? Maybe there is some alternative for Windows 8?

I was trying to get all windows using EnumWindows function and paste hwnd's to the GetAltTabInfo function and it is not working for me. I get error message: "Invalid window handle" from GetLastError, because this function (GetAltTabInfo) is no longer usable when you've got Aero enabled. This conclusion is from here: GetAltTabInfo usage?.

1

There are 1 answers

2
Codeguard On

Using "Which windows appear in the Alt+Tab list?" article by Raymond Chen, I have been able to reproduce the window list.

// See http://blogs.msdn.com/b/oldnewthing/archive/2007/10/08/5351207.aspx
BOOL IsAltTabWindow(HWND hwnd)
{
    // Start at the root owner
    HWND hwndWalk = GetAncestor(hwnd, GA_ROOTOWNER);

    // See if we are the last active visible popup
    HWND hwndTry;
    while ((hwndTry = GetLastActivePopup(hwndWalk)) != hwndTry) {
        if (IsWindowVisible(hwndTry)) break;
        hwndWalk = hwndTry;
    }
    return hwndWalk == hwnd;
}

BOOL CALLBACK CbEnumAltTab(HWND hwnd, LPARAM lParam)
{
    // Do not show invisible windows
    if (!IsWindowVisible(hwnd))
        return TRUE;

    // Alt-tab test as described by Raymond Chen
    if (!IsAltTabWindow(hwnd))
        return TRUE;

    const size_t MAX_WINDOW_NAME = 256;
    TCHAR windowName[MAX_WINDOW_NAME];
    if (hwnd == GetShellWindow())
        _tcscpy_s(windowName, MAX_WINDOW_NAME, _T("Desktop"));  // Beware of localization
    else
        GetWindowText(hwnd, windowName, MAX_WINDOW_NAME);

    // Do not show windows that has no caption
    if (0 == windowName[0])
        return TRUE;

    // Print found window to debugger's output
    const size_t MAX_MESSAGE_NAME = 64 + MAX_WINDOW_NAME;
    TCHAR message[MAX_MESSAGE_NAME];
    _stprintf_s(message, MAX_MESSAGE_NAME, _T("AltTab: %08X %s\n"), hwnd, windowName);
    OutputDebugString(message);

    return TRUE;
}

void ListAltTabWindows()
{
    EnumWindows(CbEnumAltTab, 0);
}

Notes:

  • metro seems to be excluded already
  • Didn't check WS_EX_TOOLWINDOW
  • Didn't check WS_EX_APPWINDOW.
  • Didn't test extensively