如何知道EnumWindows何时完成Windows列表?因为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;
}

给我这样的输出:

启动EnumWindows()
窗口#1():
窗口#2():
窗口#3():

EnumWindows()结束

因此EnumWindows()绝对以同步方式枚举。

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

10-13 03:21