c++ - 在 Win7 中显示像 alt-tab 这样的应用程序列表

标签 c++ winapi alt-tab

我正在尝试打印一个正在运行的应用程序列表,就像 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/

相关文章:

c++ - 在 for 循环中初始化的变量是否每次迭代都会重置?

c++ - 如何获取 Windows 控制台高度?

c++ - 获取有关 Windows 将在 C++ 中 sleep /唤醒的通知

windows - 存在任何开源 Alt-Tab 窗口切换器项目吗?

cocoa - 在 Cocoa 应用程序中以 Cmd-Tab 或 Spotlight 的形式捕获系统事件

c++ - 无法在我的程序中获得所需的输出

c++ - SDL2 线程段错误

windows - 使用 TerminateProcess 时到底有什么风险?

delphi - 应用程序有时会从 Win7 的 ALT-TAB 列表中消失

c++ - 如何使用 gdb 找出对象实例化的位置?