简体   繁体   English

为什么“ FindWindowEx”找不到RichTextBox组件

[英]Why “FindWindowEx” can't find RichTextBox component

I'm doing an auto program (C#,not C++), and I need to get a RichTextBox in a form. 我正在做一个自动程序(C#,不是C ++),我需要以表格的形式获取RichTextBox。 I have used the Spy++ to get the title and class name, but FindWindowEx always does not find RichTextBox , and GetLastError gets the word 0 . 我已经使用Spy++来获取标题和类名,但是FindWindowEx始终找不到RichTextBox ,而GetLastError得到单词0 And then this is a simple example. 然后,这是一个简单的示例。

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

https://i.stack.imgur.com/7eWil.png

But result is richtextbox can't find . 但是结果是richtextbox can't find Help me. 帮我。

I don't really think this is the best approach but it's something. 我真的不认为这是最好的方法,但确实如此。

For this specific case you can search for all the handlers in the Form and then change the one you want. 对于这种特定情况,您可以在表单中搜索所有处理程序,然后更改所需的处理程序。

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

I've tested and the allItems[1] will always be the same item, I think It's the way the items are ordered in the winForm top to bottom. 我已经测试过,allItems [1]将始终是同一项目,我认为这是在winForm中从上至下对项目进行排序的方式。

I'm using a second class for the Win Methods: 我正在为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);
}

Edit: Method to get all children windows handles taken from: https://jamesmccaffrey.wordpress.com/2013/02/03/getting-all-child-window-handles-using-c-pinvoke-findwindowex/ 编辑:从以下网址获取所有子窗口句柄的方法: https//jamesmccaffrey.wordpress.com/2013/02/03/getting-all-child-window-handles-using-c-pinvoke-findwindowex/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM