Windows API 枚举所有子窗口并打印其控件信息(包括句柄、类名、标题和 ID)

这里写自定义目录标题


#include <Windows.h>
#include
#include

// 存储控件信息的结构
struct ControlInfo {
HWND hwnd;
std::wstring className;
std::wstring title;
int id;
};

// 回调函数,用于 enumerate 子窗口
BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam) {
std::vector* pControls = reinterpret_cast<std::vector*>(lParam);

// 获取控件的类名
wchar_t className[256];
GetClassName(hwndChild, className, sizeof(className) / sizeof(wchar_t));

// 获取控件的标题
wchar_t title[256];
GetWindowText(hwndChild, title, sizeof(title) / sizeof(wchar_t));

// 获取控件的ID
int id = GetDlgCtrlID(hwndChild);

// 将控件信息添加到列表
pControls->push_back({ hwndChild, className, title, id });

return TRUE; // 继续枚举

}

void EnumAllChildWindows(HWND hwndParent) {
std::vector controls;

// 枚举父窗口的所有子窗口
EnumChildWindows(hwndParent, EnumChildProc, reinterpret_cast<LPARAM>(&controls));

// 打印控件信息
std::wcout << L"找到以下控件:" << std::endl;
for (const auto& control : controls) {
    std::wcout << L"句柄: " << control.hwnd << L", 类名: " << control.className
               << L", 标题: " << control.title << L", ID: " << control.id << std::endl;
}

}

int main() {
// 替换为您要枚举的父窗口标题
HWND hwndParent = FindWindow(NULL, L"子窗口标题"); // 获取指定窗口的句柄
if (hwndParent == NULL) {
std::cout << “未找到指定的窗口!” << std::endl;
return 1;
}

// 枚举并打印子窗口信息
EnumAllChildWindows(hwndParent);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值