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として設定する「ボタン」コントロール(Spy ++から名前を取得)の最初のハンドルを見つけることができます。

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

その親ウィンドウ内で「ボタン」コントロールの2番目3番目、または任意のハンドルを取得するにはどうすればよいですか?実際、ボタンのタイトルは異なる場合があるため、4番目のパラメーターを定義する名前で直接見つけることはできません。

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 に答える