【问题标题】:Show a list of applications like alt-tab in Win7在 Win7 中显示应用程序列表,如 alt-tab
【发布时间】:2013-12-09 06:25:55
【问题描述】:

我正在尝试打印 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;
}

【问题讨论】:

    标签: c++ winapi alt-tab


    【解决方案1】:

    你的失败在于,你应该只走可见的窗户……再读一遍博客。

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

    您的代码遍历每个窗口!

    【讨论】:

    • 天哪,我怎么会忘记这个!现在我有 6 个窗口,其中 3 个是 alt-tab 窗口,另外 3 个没有标题。我想我接下来需要做的是枚举那些特殊情况而不显示它们?
    【解决方案2】:

    只需使用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;
     }
    

    【讨论】:

    • 感谢您的回答。我尝试了您的代码,但我得到了一个不需要的窗口,我认为仍然需要遍历。 GetWindowTextLength 真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 2016-10-10
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多