我正在尝试打印一个正在运行的应用程序列表,就像 alt-tab 会给我的一样。以下是我到目前为止所做的:
1.一开始我尝试了EnumWindows,但我得到了数百个条目。
2.我发现了一些类似的问题,他们把我带到了 Raymond Chen 的博客。 http://blogs.msdn.com/b/oldnewthing/archive/2007/10/08/5351207.aspx
但是它仍然显示超过 100 个窗口(window_num1 是 158,window_num2 是 329),而 alt-tab 只会给我 4 个。我做错了什么?这是我的代码:
#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))
break;
hwndWalk = hwndTry;
}
return hwndWalk == hwnd;
}
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
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);
window_num1++;
}
window_num2++;
//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()
{
MyFunc();
cout<<endl<<window_num1<<endl<<window_num2;
return 0;
}
最佳答案
你的失败是,你应该只走可见的 window ......再读一遍博客。
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.
您的代码遍历每个窗口!
关于c++ - 在 Win7 中显示像 alt-tab 这样的应用程序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464448/