2011-04-15 44 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++