2011-04-15 43 views
3

根據http://msdn.microsoft.com/en-us/library/ms633500(v=vs.85).aspx我定義了FindWindowEx函數。C#根據名稱和序號使用FindWindowEx獲取子句柄

using System.Runtime.InteropServices; 

[DllImport("user32.dll", CharSet=CharSet.Unicode)] 
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle); 

現在我能找到第一手柄「按鍵」控制(從間諜獲取名稱++)設置childAfter爲IntPtr.Zero

IntPtr hWndParent = new IntPtr(2032496); // providing parent window handle 
IntPtr hWndButton = FindWindowEx(hWndParent, IntPtr.Zero, "Button", string.Empty); 

如何獲得第二第三或父窗口裏面的「按鈕」控件的什麼把柄?事實是,按鈕標題可能會有所不同,所以我無法直接通過名稱來定義第四個參數。

+0

請重新表述您的問題對我們理解。 – 2011-04-15 07:59:33

+0

爲什麼不使用UIAutomation命名空間? – 2012-07-22 16:57:44

+0

我的解決方案可以用於非託管C++程序以及代碼片段是完全相同的:)這是爲了挖掘/黑客攻擊,而不是用於測試目的。不喜歡研究中的任何隨時可用的自動化。 – 84RR1573R 2012-07-22 21:55:14

回答

10
static IntPtr FindWindowByIndex(IntPtr hWndParent, int index) 
{ 
    if (index == 0) 
     return hWndParent; 
    else 
    { 
     int ct = 0; 
     IntPtr result = IntPtr.Zero; 
     do 
     { 
      result = FindWindowEx(hWndParent, result, "Button", null); 
      if (result != IntPtr.Zero) 
       ++ct; 
     } 
     while (ct < index && result != IntPtr.Zero); 
     return result; 
    } 
} 

使用像:

IntPtr hWndThirdButton = FindWindowByIndex(hWnd, 3); // handle of third "Button" as shown in Spy++