C#得到孩子处理使用FindWindowEx按名称和序号 [英] C# get child handles using FindWindowEx by name and ordinal number

查看:1695
本文介绍了C#得到孩子处理使用FindWindowEx按名称和序号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://msdn.microsoft .COM / EN-US /库/ ms633500(v = vs.85)的.aspx 我定义FindWindowEx的功能。

 ; 

函数[DllImport(user32.dll中,字符集= CharSet.Unicode)]
静态外部的IntPtr FindWindowEx(IntPtr的parentHandle,IntPtr的childAfter,串lclassName,串WINDOWTITLE);

现在我能找到的第一按钮控件的句柄(摆脱间谍++名称)设置为childAfter IntPtr.Zero

  IntPtr的hWndParent =新的IntPtr(2032496 ); //提供父窗口的句柄
IntPtr的hWndButton = FindWindowEx(hWndParent,IntPtr.Zero,按钮,的String.Empty);



如何获得的第二 第三或该父窗口内的按钮控件的什么把柄?事实是,按钮标题可能会有所不同,所以我不能直接通过名称定义的第四个参数中找到它们。


解决方案

 静态的IntPtr FindWindowByIndex(IntPtr的hWndParent,INT指数)
{
如果(指数== 0)
返回hWndParent;
,否则
{
INT克拉= 0;
IntPtr的结果= IntPtr.Zero;

{
结果= FindWindowEx(hWndParent,结果,按钮,NULL);
如果(结果= IntPtr.Zero!)
++克拉;
}
,而(CT<&指数放大器;&安培;!结果= IntPtr.Zero);
返回结果;
}
}

使用这样的:

  IntPtr的hWndThirdButton = FindWindowByIndex(HWND,3); //为Spy中第三个按钮的手柄+ 


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.

解决方案

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++

这篇关于C#得到孩子处理使用FindWindowEx按名称和序号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆