inquam
inquam

Reputation: 12932

Insert text into the textbox of another application

How do I, using C# or C++, insert text into the textbox of another application? I did this a long time ago and seemed to remember something about using the applications HWND. But since that change for every instance of the application I feel that I fon't remember the complete story. Do I somehow get a list of running apps, extract the one I want, get the HWND from that and then... hmm.... then what? :)

Upvotes: 13

Views: 25079

Answers (4)

Roblox
Roblox

Reputation: 57

Instead of targeting a specific app you could just send keystrokes to the text field.

  private void button1_Click(object sender, EventArgs e)
    {
       System.Threading.Thread.Sleep(5000);
       SendKeys.Send(send_text);

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        send_text = textBox1.Text;            
    }

Upvotes: 2

Xavier Poinas
Xavier Poinas

Reputation: 19733

Use FindWindowEx() to find the handle (HWND) and then send the WM_SETTEXT message using SendMessage()

When using FindWindowEx you will need to first find the main window handle by using its class name. Then you will need to find the handle of whatever container the textbox is in, calling FindWindowEx, passing the handle of the parent (the window), and the class name of the container. You will need to repeat this until you reach the textbox. You can use a tool called Spy++ that is installed by default with Visual Studio to inspect the target application and find out the hierarchy of containers (all objects are really called windows in the API but I'm calling them containers in contrast with the top-level window) with their class names.

Upvotes: 17

TalentTuner
TalentTuner

Reputation: 17556

you can use ClipBoard class to achieve the same

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941695

Then SendMessage(), WM_SETTEXT

Upvotes: 3

Related Questions