【问题标题】:C# get child handles using FindWindowEx by name and ordinal numberC# 使用 FindWindowEx 按名称和序号获取子句柄
【发布时间】:2011-08-06 02:20:13
【问题描述】:

根据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”控件的 first 句柄(从 Spy++ 获取名称)。

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

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

【问题讨论】:

  • 请重新表述您的问题以便我们理解。
  • 为什么不使用 UIAutomation 命名空间?
  • 我的解决方案可用于非托管 C++ 程序,sn-p 也是如此 :) 这是用于挖掘/黑客攻击,而不是用于测试目的。不喜欢研究中的任何即用型自动化。

标签: c# winapi visual-c++ handle spy++


【解决方案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;
    }
}

像这样使用:

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

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2010-11-30
    相关资源
    最近更新 更多