7

根据http://msdn.microsoft.com/en-us/library/ms633500(v=vs.85).aspx我定义了 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的“Button”控件的第一个句柄(从 Spy++ 获取名称) 。

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

如何在该父窗口中获取“按钮”控件的第二个第三个或任何句柄?事实是,按钮标题可能会有所不同,因此我无法通过定义第四个参数的名称直接找到它们。

4

1 回答 1

20
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++
于 2011-04-16T09:55:17.307 回答