user2818068 user2818068 -3 years ago 137
C++ Question

Show a list of applications like alt-tab in Win7

I am trying to print a list of running applications like alt-tab would give me. Here are what I have done so far:

1.In the beginning I tried EnumWindows, but I got hundreds of entries.

2.I found some similar questions and they led me to the blog of Raymond Chen.

However it still shows more than 100 windows(window_num1 being 158 and window_num2 being 329), while alt-tab would give me only 4. What did I do wrong? Here is my code:

#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "user32.lib")

HWND windowHandle;
int window_num1=0;
int window_num2=0;

BOOL IsAltTabWindow(HWND hwnd)
if (hwnd == GetShellWindow()) //Desktop
return false;
// 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))
hwndWalk = hwndTry;
return hwndWalk == hwnd;

TCHAR title[500];
ZeroMemory(title, sizeof(title));

//string strTitle;

GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));

if (IsAltTabWindow(hWnd))
_tprintf(_T("Value is %s\n"), title);

//strTitle += title; // Convert to std::string
if(_tcsstr(title, _T("Excel")))
windowHandle = hWnd;
return FALSE;
return TRUE;

void MyFunc(void) //(called by main)
EnumWindows(MyEnumProc, 0);

int main()
return 0;

Answer Source

You failure is, that you should walk only visible windows... read the blog again.

For each visible window, walk up its owner chain until you find the root owner. Then walk back down the visible last active popup chain until you find a visible window. If you're back to where you're started, then put the window in the Alt+↹Tab list.

Your code walks over every window!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download