按类查找子窗口

在第三方应用程序中选择照片时,我无法获取“编辑”字段句柄。

Spy++ 显示一切正确,但FindWindow失败。我可以获得父窗口本身的句柄。我想我需要寻找子窗口。我可以得到一些句柄GetWindow,但不清楚它们是什么。窗口标题为空。FindWindowEx根本不起作用,返回0。我这样指示:

IntPtr hwndchild = (hwnd, IntPtr.Zero, null, "Edit")

https://img2.mukewang.com/65055f9700013eba03420116.jpg

萧十郎
浏览 138回答 3
3回答

慕码人2483693

根据您提供的屏幕截图,仅使用FindWindow/Ex()函数,您可以获得编辑控件的 HWND,如下所示:IntPtr hwndDlg = FindWindow(null, "Choose an image"); IntPtr hwndCBEx = FindWindowEx(hwndDlg, IntPtr.Zero, "ComboBoxEx32", null); IntPtr hwndCB = FindWindowEx(hwndCBEx, IntPtr.Zero, "ComboBox", null); IntPtr hwndEdit = FindWindowEx(hwndCB, IntPtr.Zero, "Edit", null);但是,一旦获得了 ComboBoxEx 控件的 HWND,获取其 Edit 控件的 HWND 的正确CBEM_GETEDITCONTROL方法是使用以下消息:const int CBEM_GETEDITCONTROL = 1031; IntPtr hwndDlg = FindWindow(null, "Choose an image"); IntPtr hwndCBEx = FindWindowEx(hwndDlg, IntPtr.Zero, "ComboBoxEx32", null); IntPtr hwndEdit = SendMessage(hwndCBEx, CBEM_GETEDITCONTROL, 0, 0);请注意,对于标准 ComboBox 控件(可以使用CBEM_GETCOMBOCONTROL消息从 ComboBoxEx 控件获取),可以使用CB_GETCOMBOBOXINFO消息或GetComboBoxInfo()函数。该字段中返回编辑控件的 HWND COMBOBOXINFO.hwndItem。

BIG阳

如果您正在寻找父窗口的子窗口,您应该使用 EnumChildWindows。以下是 C++ 代码,但可以轻松调用:您可以将委托编组为回调的函数指针。std::vector<HWND> FindChildrenByClass(HWND parent, const std::string& target_class){&nbsp; &nbsp; struct EnumWndParam {&nbsp; &nbsp; &nbsp; &nbsp; std::vector<HWND> output;&nbsp; &nbsp; &nbsp; &nbsp; std::string target;&nbsp; &nbsp; } enum_param;&nbsp; &nbsp; enum_param.target = target_class;&nbsp; &nbsp; EnumChildWindows(&nbsp; &nbsp; &nbsp; &nbsp; parent,&nbsp; &nbsp; &nbsp; &nbsp; [](HWND wnd, LPARAM lparam) -> BOOL {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; auto param = reinterpret_cast<EnumWndParam*>(lparam);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char class_name[512];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetClassName(wnd, class_name, 512);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (param->target == class_name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param->output.push_back(wnd);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return TRUE;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; reinterpret_cast<LPARAM>(&enum_param)&nbsp; &nbsp; );&nbsp; &nbsp; return enum_param.output;}int main(){&nbsp; &nbsp; auto windows = FindChildrenByClass( reinterpret_cast<HWND>(0x0061024A), "Edit");&nbsp; &nbsp; for (auto wnd : windows) {&nbsp; &nbsp; &nbsp; &nbsp; std::cout << std::hex << wnd << std::endl;&nbsp; &nbsp; }}请注意,上面我没有在回调 lambda 中递归调用 FindChildrenByClass。这不是一个错误。EnumChildWindows 已经执行了此递归。它在父窗口的子窗口和孙子窗口等上运行,开箱即用,无需您指定此行为或实现它。

宝慕林4294392

就像有人已经假设的那样。尝试 EnumChildWindow 方法。
打开App,查看更多内容
随时随地看视频慕课网APP