C# get child handles using FindWindowEx by name and ordinal number

回眸只為那壹抹淺笑 提交于 2019-12-17 16:25:07

问题


According to http://msdn.microsoft.com/en-us/library/ms633500(v=vs.85).aspx I define FindWindowEx function.

using System.Runtime.InteropServices;

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

Now I am able to find first handle of "Button" control (get name from Spy++) setting childAfter as IntPtr.Zero.

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

How to get second, third or any handle of "Button" control inside that parent window? The fact is, button titles may vary, so I cannot find them directly by name defining fourth parameter.


回答1:


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;
    }
}

Use like:

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


来源:https://stackoverflow.com/questions/5673099/c-sharp-get-child-handles-using-findwindowex-by-name-and-ordinal-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!