#include <Windows.h> #include <iostream> #include <string> static BOOL CALLBACK enumchildWindowCallback(HWND hWnd, LPARAM lparam) { int length = GetWindowTextLength(hWnd); char * buffer = new char[length + 1]; GetWindowText(hWnd, buffer, length + 1); std::string windowTitle(buffer); std::cout << hWnd << ": " << windowTitle << std::endl; return TRUE; } int main() { HWND hwnd = (HWND)0x000D0DEE; //父窗口的句柄 EnumChildWindows(hwnd, enumchildWindowCallback, NULL); std::cin.ignore(); return 0; }
拓展: 使用EnumWindows枚举窗口句柄(不包括子窗口)
#include <Windows.h> #include <string> #include <iostream> static BOOL CALLBACK enumchildWindowCallback(HWND hWnd, LPARAM lparam) { int length = GetWindowTextLength(hWnd); char* buffer = new char[length + 1]; GetWindowText(hWnd, buffer, length + 1); std::string windowTitle(buffer); int n = (int)hWnd; // Ignore windows if invisible or missing a title // if (IsWindowVisible(hWnd) && length != 0) { std::cout << n << ": " << windowTitle << std::endl; // } return TRUE; } static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM m) { TCHAR _classbuf[255]; int length = GetWindowTextLength(hWnd); char* buffer = new char[length + 1]; GetWindowText(hWnd, buffer, length + 1); std::string windowTitle(buffer); m = GetClassName(hWnd, _classbuf, 1024); int n = (int)hWnd; // Ignore windows if invisible or missing a title // if (IsWindowVisible(hWnd) && length != 0) { std::cout << n << ": " << windowTitle << std::endl; // EnumChildWindows(hWnd, enumWindowCallback, m); // } return TRUE; } int main() { std::cout << "Enmumerating windows..." << std::endl; EnumWindows(enumWindowCallback, NULL); std::cin.ignore(); return 0; }
如果去掉EnumChildWindows(hWnd, enumWindowCallback, m)的注释,将会枚举所有的窗口
原文:https://www.cnblogs.com/strive-sun/p/12653803.html