C# 使用 FindWindowEx 按名称和序号获取子句柄 [英] C# get child handles using FindWindowEx by name and ordinal number

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

问题描述

根据 http://msdn.microsoft.com/en-us/library/ms633500(v=vs.85).aspx 我定义了 FindWindowEx 函数.

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

现在我可以找到Button"控件的第一个句柄(从 Spy++ 获取名称)将 childAfter 设置为 IntPtr.Zero.

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

像这样使用:

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

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

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