Windows程序设计之枚举所有屏幕上的顶层窗口的方法

一、函数原型

BOOL EnumWindows(
  WNDENUMPROC lpEnumFunc,  // callback function
  LPARAM lParam            // application-defined value
);

int GetWindowText(
  HWND hWnd,        // handle to window or control
  LPTSTR lpString,  // text buffer
  int nMaxCount     // maximum number of characters to copy
);

二、示例代码

#include <Windows.h>
#include <locale.h>
#include <stdio.h>
#include <tchar.h>

//BOOL WINAPI EnumWindows(_In_ WNDENUMPROC lpEnumFunc, _In_ LPARAM lParam);
//typedef BOOL(CALLBACK* WNDENUMPROC)(HWND, LPARAM);

BOOL CALLBACK WinEnumProc(HWND hWnd, LPARAM lParam)
{
    TCHAR data[MAX_PATH] = { 0 };
    GetWindowText(hWnd, data, MAX_PATH);
    if (_tcslen(data) > 0)
    {
        _tprintf(TEXT("%s\n"), data);
    }
    return TRUE;
}

int main(void)
{
    _tsetlocale(LC_ALL, TEXT("CHS"));

    EnumWindows(WinEnumProc, NULL);

    return EXIT_SUCCESS;
}

Leave a Reply

Your email address will not be published.