[英]FindWindowEx returns 0 when used on specific application
我试图找到给定窗口的所有子控件。 我可以使用Inspect.exe
(来自Windows开发工具包)验证窗口的句柄。 问题是,当我调用FindWindowEx
,函数返回0( IntPtr.Zero
是IntPtr.Zero
),而我可以使用Inspect.exe
找到控件。
这是我的代码
[DllImport("user32.dll", SetLastError=true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle,
IntPtr childAfter, string className, string windowTitle);
public static List<IntPtr> EnumChildren(IntPtr hwnd)
{
IntPtr zero = IntPtr.Zero;
List<IntPtr> list = new List<IntPtr>();
do
{
zero = FindWindowEx(hwnd, zero, null, null); // Returns 0
if (zero != IntPtr.Zero)
{
list.Add(zero);
}
}
while (zero != IntPtr.Zero);
return list;
}
我尝试过使用以下内容,它们都返回0:
zero = FindWindowEx(hwnd, zero, "TextBox", null);
zero = FindWindowEx(hwnd, zero, "TextBox", "Text");
zero = FindWindowEx(hwnd, zero, String.Empty, String.Empty);
zero = FindWindowEx(hwnd, zero, "TextBox", String.Empty);
我知道有一种方法可以找到窗口的控件,因为Inspect.exe
正在这样做。 我尝试过使用EnumChildWindows
但获得相同的结果,例如一个空列表。 请注意,使用其他软件(到目前为止我已经尝试过Thunderbird和KeePass), FindWindowEx
函数可以正常工作,而不是我必须使用的应用程序。
我已经使用EnumChildWindows
进行了测试,以确保只有一个窗口具有我正在寻找的标题,而且它是唯一的窗口。 我真的无法解释为什么我无法获得任何控件。
我做错了什么,是否有另一种方法来获得给定窗口的所有子窗口?
你在打电话:
zero = FindWindowEx(hwnd, zero, null, null);
这将返回0.因为您为类名和窗口名传递NULL
, FindWindowEx
考虑hwnd
所有子项。 由于您为hwndChildAfter
传递了NULL
,因此文档告诉您:
如果hwndChildAfter为NULL,则搜索从hwndParent的第一个子窗口开始。
换句话说,唯一可以得出的结论是:
hwnd
参数无效,或 hwnd
指定的窗口没有子窗口。 现在,对于第1点,我们假设您能够提供有效的窗口句柄。 在这种情况下,唯一剩下的可能性是hwnd
没有孩子。 这很合情合理。 许多GUI框架使用非窗口控件。 这也符合EnumChildWindows
返回窗口的事实。
您已使用Inspect工具查看应用程序。 让我们看看MSDN对Inspect的看法。
Inspect(Inspect.exe)是一个基于Windows的工具,可以选择任何UI元素并查看元素的辅助功能数据。 您可以查看Microsoft UI自动化属性和控件模式,以及Microsoft Active Accessibility属性。 Inspect还允许您测试UI Automation树中自动化元素的导航结构,以及Microsoft Active Accessibility层次结构中的可访问对象。
根本问题是你选择了错误的工具来解决这个问题。 您应该使用自动化API来执行此任务,而不是使用窗口层次结构。 这就是Inspect能够分解这个应用程序的控件的方式,也必须这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.