gpt4 book ai didi

C# 使用 FindWindowEx 按名称和序号获取子句柄

转载 作者:太空狗 更新时间:2023-10-29 22:14:48 30 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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 按名称和序号获取子句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5673099/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com