2

I'm doing an auto program (C#,not C++), and I need to get a RichTextBox in a form. 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. 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. Help me.

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
  • Syntax error."richtextobx can't be found". – perfect puzzle Jun 01 '18 at 08:16
  • 2
    Rich text edit controls don't use the window title to store their text, so you can't use `FindWindowEx` to find them by title. You can search by class, or use Spy++ to find the control's ID and then use `GetDlgItem` to find it by ID. – Jonathan Potter Jun 01 '18 at 08:17
  • FWIW that's not a syntax error. Syntax errors are emitted by the compiler. Your error is a runtime error. – David Heffernan Jun 01 '18 at 08:40
  • My English is very bad."richtextbox can't find" should be a error. – perfect puzzle Jun 01 '18 at 08:42
  • 3
    Wrong solution. Use [UI Automation](https://learn.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview), and navigate the accessible tree instead. If nothing else, this won't fail for window-less controls. – IInspectable Jun 01 '18 at 09:34

1 Answers1

0

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.

I'm using a second class for the Win Methods:

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/

Diogo Neves
  • 556
  • 4
  • 14
  • Oh.This example just is a example.Only to say `FindWindowEx` and `RichTextBox` not work. – perfect puzzle Jun 01 '18 at 09:17
  • 1
    Failing to identify the control you want to modify, you decide to change **all** controls? This totally qualifies as not being *"the best approach"*. I strongly recommend deleting this post. It's just so wrong beyond hope of regeneration. – IInspectable Jun 01 '18 at 11:21
  • 1
    For the sake of trying to help I'll not remove the post, but thank you for clarifying that this is not the best approach. – Diogo Neves Jun 02 '18 at 16:16