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");
}
But result is 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.
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:
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/