简体   繁体   中英

How to know when EnumWindows finishes its listing of windows?

How to know when EnumWindows finishes its listing of windows? Because EnumWindows receives a callback function as parameter, and it keeps calling it until no more windows to be listed.

EnumWindows() blocks while enumeration is taking place. When EnumWindows() finishes enumerating through the windows, it returns a BOOL .

The following code snippet:

#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;
}

gives me an output like this:

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

So EnumWindows() definitely enumerates in a synchronous manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM