簡體   English   中英

在特定應用程序上使用時,FindWindowEx返回0

[英]FindWindowEx returns 0 when used on specific application

我試圖找到給定窗口的所有子控件。 我可以使用Inspect.exe (來自Windows開發工具包)驗證窗口的句柄。 問題是,當我調用FindWindowEx ,函數返回0( IntPtr.ZeroIntPtr.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.因為您為類名和窗口名傳遞NULLFindWindowEx考慮hwnd所有子項。 由於您為hwndChildAfter傳遞了NULL ,因此文檔告訴您:

如果hwndChildAfter為NULL,則搜索從hwndParent的第一個子窗口開始。

換句話說,唯一可以得出的結論是:

  1. hwnd參數無效,或
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM