C# 为什么;FindWindowEx“;can';找不到RichTextBox组件

C# 为什么;FindWindowEx“;can';找不到RichTextBox组件,c#,winforms,winapi,richtextbox,findwindowex,C#,Winforms,Winapi,Richtextbox,Findwindowex,我正在做一个自动程序(C#,而不是C++),我需要一个表单中的RichTextBox。我使用了Spy++获取标题和类名,但是FindWindowEx始终找不到RichTextBox,而GetLastError获取单词0。这是一个简单的例子 IntPtr parent = FindWindow(null, "Form1"); if (parent!=IntPtr.Zero) { //find test1 textbox IntPtr child = FindWindowEx(par

我正在做一个自动程序(C#,而不是C++),我需要一个表单中的RichTextBox。我使用了
Spy++
获取标题和类名,但是
FindWindowEx
始终找不到
RichTextBox
,而
GetLastError
获取单词
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");
}


但结果是,
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);
}
公共类Win32
{
公共常量int WM_SETTEXT=0X000C;
公共静态列表GetAllChildrenWindowHandles(IntPtr hParent、int maxCount)
{
var result=新列表();
int-ct=0;
IntPtr prevChild=IntPtr.Zero;
IntPtr currChild=IntPtr.Zero;
while(true&&ct

编辑:获取所有子窗口句柄的方法:

我真的不认为这是最好的方法,但这确实是一个好方法

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

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);
}
公共类Win32
{
公共常量int WM_SETTEXT=0X000C;
公共静态列表GetAllChildrenWindowHandles(IntPtr hParent、int maxCount)
{
var result=新列表();
int-ct=0;
IntPtr prevChild=IntPtr.Zero;
IntPtr currChild=IntPtr.Zero;
while(true&&ct

Edit:从以下位置获取所有子窗口句柄的方法:

语法错误。“找不到richtextobx”。富文本编辑控件不使用窗口标题来存储其文本,因此您不能使用
FindWindowEx
按标题查找它们。您可以按类搜索,或者使用Spy++查找控件的ID,然后使用
GetDlgItem
按ID查找它。FWIW这不是语法错误。语法错误由编译器发出。您的错误是运行时错误。我的英语很差。“richtextbox找不到”应该是错误。错误的解决方案。使用,然后导航可访问树。如果没有其他问题,对于无窗口控件,这不会失败。语法错误。“找不到richtextobx”。富文本编辑控件不使用窗口标题来存储其文本,因此您不能使用
FindWindowEx
按标题查找它们。您可以按类搜索,或者使用Spy++查找控件的ID,然后使用
GetDlgItem
按ID查找它。FWIW这不是语法错误。语法错误由编译器发出。您的错误是运行时错误。我的英语很差。“richtextbox找不到”应该是错误。错误的解决方案。使用,然后导航可访问树。如果没有其他问题,这对于无窗口控件不会失败。哦,这个示例只是一个示例。只是说
FindWindowEx
RichTextBox
不起作用。如果无法识别要修改的控件,您决定更改所有控件吗?这完全不是“最佳方法”。我强烈建议删除此帖子。这是一个错误,无法再生。为了帮助我,我不会删除帖子,但感谢你澄清这不是最好的方法。哦,这个例子就是一个例子。只是说
FindWindowEx
RichTextBox
不起作用。无法识别要修改的控件,您决定更改所有控件吗?这完全不是“最佳方法”。我强烈建议删除此帖子。这是一个错误的想法,我不想帮你,但是谢谢你澄清这不是最好的方法。