c++ - 如何知道 EnumWindows 何时完成其窗口列表?

标签 c++ windows visual-c++

如何知道 EnumWindows 何时完成其窗口列表?因为 EnumWindows 接收一个回调函数作为参数,它会一直调用它,直到没有更多的窗口被列出。

最佳答案

EnumWindows() 在进行枚举时阻塞。当 EnumWindows() 完成对窗口的枚举时,它返回一个 BOOL

以下代码片段:

#include <windows.h>
#include <cstdio>

BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam)
{
    int& i = *(reinterpret_cast<int*>(lparam));
    ++i;
    char title[256];
    ::GetWindowText(hwnd, title, sizeof(title));
    ::printf("Window #%d (%x): %s\n", i, hwnd, title);
    return TRUE;
}

int main()
{
    int i = 0;
    ::printf("Starting EnumWindows()\n");
    ::EnumWindows(&MyEnumWindowsProc, reinterpret_cast<LPARAM>(&i));
    ::printf("EnumWindows() ended\n");
    return 0;
}

给我这样的输出:

Starting EnumWindows()
Window #1 (<hwnd>): <title>
Window #2 (<hwnd>): <title>
Window #3 (<hwnd>): <title>
<and so on...>
EnumWindows() ended

所以 EnumWindows() 肯定是以同步方式枚举。

关于c++ - 如何知道 EnumWindows 何时完成其窗口列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7250462/

相关文章:

python - 通过 python win32com 检索 outlook 联系人

c++ - 小单元测试

c - 如何防止 Visual C 编译器优化掉 "unused"全局变量

c++ - 视觉 C++ : No default constructor

c++ - 用于清理 vector 破坏的模板 - 怎么做?

java - ANT_HOME 设置正确但无法运行 ant

c++ - 如何在Win32 C++中进行打印预览?

c++ - 修复 C4150 : deletion of pointer to incomplete type COldStuff without including header or changing old code

c++ - MFC:如何创建带有透明 PNG 作为背景(而不是窗口镶边)的 Skinnied Dialog?

c++ - 如何在 VS Code 中设置 C++ Testmate