0

我正在尝试打印正在运行的应用程序列表,例如 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;
}
4

2 回答 2

3

你失败了,你应该只走可见的窗户......再次阅读博客。

对于每个可见窗口,沿着其所有者链向上走,直到找到根所有者。然后沿着可见的最后一个活动弹出链往回走,直到找到一个可见的窗口。如果您回到开始的位置,则将窗口放在Alt+↹Tab列表中。

您的代码遍历每个窗口!

于 2013-12-09T07:46:32.920 回答
0

只需使用IsWindowVisible

BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
    TCHAR title[256] = {0,};
    if (IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) > 0)
    {
       window_num1++;
       GetWindowText(hWnd, title, _countof(title));
       _tprintf(_T("Value is %d, %s\n"), window_num1, title);
    }
    return TRUE;
 }
于 2013-12-09T11:10:01.390 回答