简体   繁体   English

在特定应用程序上使用时,FindWindowEx返回0

[英]FindWindowEx returns 0 when used on specific application

I am trying to find all child controls of a given window. 我试图找到给定窗口的所有子控件。 I can get the window's handle, which I have verified using Inspect.exe (from the Windows Development Kit). 我可以使用Inspect.exe (来自Windows开发工具包)验证窗口的句柄。 The problem is when I call FindWindowEx the function returns 0 ( IntPtr.Zero to be precise) while I can find the controls with Inspect.exe . 问题是,当我调用FindWindowEx ,函数返回0( IntPtr.ZeroIntPtr.Zero ),而我可以使用Inspect.exe找到控件。

Here is my code 这是我的代码

[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;
}

I have tried using the following, which all return 0 as well: 我尝试过使用以下内容,它们都返回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);

I know there is a way to find the window's controls as Inspect.exe is doing it. 我知道有一种方法可以找到窗口的控件,因为Inspect.exe正在这样做。 I have tried using EnumChildWindows but get the same result, eg an empty list. 我尝试过使用EnumChildWindows但获得相同的结果,例如一个空列表。 Note that with other software (I have tried Thunderbird and KeePass so far) the FindWindowEx function works properly, just not with the application I have to work with. 请注意,使用其他软件(到目前为止我已经尝试过Thunderbird和KeePass), FindWindowEx函数可以正常工作,而不是我必须使用的应用程序。

I have tested using EnumChildWindows to make sure there is only one window with the title I am looking for and it is the only one. 我已经使用EnumChildWindows进行了测试,以确保只有一个窗口具有我正在寻找的标题,而且它是唯一的窗口。 I really can't explain why I can't get any of its controls. 我真的无法解释为什么我无法获得任何控件。

What am I doing wrong and is there another way to get all child windows for a given window? 我做错了什么,是否有另一种方法来获得给定窗口的所有子窗口?

You are calling: 你在打电话:

zero = FindWindowEx(hwnd, zero, null, null);

And this returns 0. Since you pass NULL for both class name and window name, FindWindowEx considers all children of hwnd . 这将返回0.因为您为类名和窗口名传递NULLFindWindowEx考虑hwnd所有子项。 Since you pass NULL for hwndChildAfter , the documentation tells you that: 由于您为hwndChildAfter传递了NULL ,因此文档告诉您:

If hwndChildAfter is NULL, the search begins with the first child window of hwndParent. 如果hwndChildAfter为NULL,则搜索从hwndParent的第一个子窗口开始。

In other words, the only conclusions that can be drawn are that either: 换句话说,唯一可以得出的结论是:

  1. The hwnd parameter is invalid, or hwnd参数无效,或
  2. The window specified by hwnd has no child windows. hwnd指定的窗口没有子窗口。

Now, for point 1, let's assume that you are capable of supplying a valid window handle. 现在,对于第1点,我们假设您能够提供有效的窗口句柄。 In which case the only remaining possibility is that hwnd has no children. 在这种情况下,唯一剩下的可能性是hwnd没有孩子。 That is quite plausible. 这很合情合理。 Many GUI frameworks use non-windowed controls. 许多GUI框架使用非窗口控件。 That also tallies with the fact that EnumChildWindows returns no windows. 这也符合EnumChildWindows返回窗口的事实。

You've used the Inspect tool to look at the application. 您已使用Inspect工具查看应用程序。 Let's see what MSDN says about Inspect . 让我们看看MSDN对Inspect的看法。

Inspect (Inspect.exe) is a Windows-based tool that enables you select any UI element and view the element's accessibility data. Inspect(Inspect.exe)是一个基于Windows的工具,可以选择任何UI元素并查看元素的辅助功能数据。 You can view Microsoft UI Automation properties and control patterns, as well as Microsoft Active Accessibility properties. 您可以查看Microsoft UI自动化属性和控件模式,以及Microsoft Active Accessibility属性。 Inspect also enables you to test the navigational structure of the automation elements in the UI Automation tree, and the accessible objects in the Microsoft Active Accessibility hierarchy. Inspect还允许您测试UI Automation树中自动化元素的导航结构,以及Microsoft Active Accessibility层次结构中的可访问对象。

The fundamental problem is that you have chosen the wrong tool to solve this problem. 根本问题是你选择了错误的工具来解决这个问题。 Instead of poking around with window hierarchies, you should use the automation API to perform this task. 您应该使用自动化API来执行此任务,而不是使用窗口层次结构。 That is how Inspect is able to decompose the controls of this application and will have to do so also. 这就是Inspect能够分解这个应用程序的控件的方式,也必须这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM