gpt4 book ai didi

c# - 为什么 "FindWindowEx"找不到RichTextBox组件

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:16 24 4
gpt4 key购买 nike

我正在编写一个自动程序(C#,而不是 C++),我需要在表单中获取 RichTextBox。我用Spy++获取标题和类名,但是FindWindowEx总是找不到RichTextBoxGetLastError 获取单词 0。这是一个简单的例子。

IntPtr parent = FindWindow(null, "Form1");
if (parent!=IntPtr.Zero) {
//find test1 textbox
IntPtr child = FindWindowEx(parent, 0,null, "test1");
if (child!=IntPtr.Zero) {
SendMessage(child, 0x000c, 0, lParam: "test");
} else {
Console.WriteLine("textbox can't be found");
}
//find test2 richtextbox
IntPtr childRich = FindWindowEx(parent, 0, null, "test2");
if (childRich != IntPtr.Zero) {
SendMessage(child, 0x000c, 0, lParam: "test");
} else {
Console.WriteLine("richtextbox can't be found");
}
} else {
Console.WriteLine("Form1 can't be found");
}

/image/7eWil.png

但结果是richtextbox 找不到。帮助我。

最佳答案

我真的不认为这是最好的方法,但它是某种东西。

对于这种特定情况,您可以搜索表单中的所有处理程序,然后更改所需的处理程序。

var iHandle = Win32.FindWindow(null, "Form1");
var allItems = Win32.GetAllChildrenWindowHandles((IntPtr)iHandle, int.MaxValue);
Win32.SendMessage(allItems[1], 0x000c, 0, lParam: "Now you can change the text!");

我已经测试过,allItems[1] 将始终是相同的项目,我认为这是项目在 winForm 中从上到下排序的方式。

我正在为 Win 方法使用第二个类:

public class Win32
{
public const int WM_SETTEXT = 0X000C;

public static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
{
var result = new List<IntPtr>();
int ct = 0;
IntPtr prevChild = IntPtr.Zero;
IntPtr currChild = IntPtr.Zero;
while (true && ct < maxCount)
{
currChild = FindWindowEx(hParent, prevChild, null, null);
if (currChild == IntPtr.Zero) break;
result.Add(currChild);
prevChild = currChild;
++ct;
}
return result;
}

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("User32.dll")]
public static extern int FindWindow(string strClassName, string strWindowName);
}

编辑:获取所有子窗口句柄的方法:https://jamesmccaffrey.wordpress.com/2013/02/03/getting-all-child-window-handles-using-c-pinvoke-findwindowex/

关于c# - 为什么 "FindWindowEx"找不到RichTextBox组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50638850/

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