Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,158 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,947 people online right now. Registration is fast and FREE... Join Now!




How do I put text in another program?

 
Reply to this topicStart new topic

How do I put text in another program?

Demon4231
23 Dec, 2007 - 11:15 PM
Post #1

New D.I.C Head
*

Joined: 23 Dec, 2007
Posts: 8

I am trying to put text in another program and when I use notepad it works:



CODE
private void button2_Click(object sender, EventArgs e)

{ string lpClassName = "Notepad";

string lpWindowName = "Untitled - Notepad";

IntPtr hWnd = FindWindow(lpClassName, lpWindowName);

SetForegroundWindow(hWnd); string z = "TEXT"; const int WM_SETTEXT = 0x0C; SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, z);

SendKeys.Send("This is my text");

}



That works fine but when I try:



CODE
private void button2_Click(object sender, EventArgs e)

{ string lpClassName = "Wordpad";

string lpWindowName = "Document1 - WordPad";

IntPtr hWnd = FindWindow(lpClassName, lpWindowName);

SetForegroundWindow(hWnd); string z = "TEXT";

const int WM_SETTEXT = 0x0C; SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, z);

SendKeys.Send("This is my text");

}




it either just stalls, puts "This is my text" into the textbox in my program, or just puts it in notepad (if it's open). What should the ClassName be? Is it the exe that is running or the default name or the program?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: How Do I Put Text In Another Program?
24 Dec, 2007 - 11:19 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well there are two things here that can go wrong, you specify the wrong title of the document (I noticed you had document1 when it appears the first document is just "Document - Wordpad") and the second thing is specifying a classname which is improper.

Now there are two ways to solve the second problem. First you can use a program like Spy++ that came with Visual studio in the earlier editions... this will allow you to find the classnames for running applications. The second way is to just pass "null" for the classname and let findwindow use the document title.

Not all programs have a classname and thus can be a problem. However if you have a title that changes constantly you may want to check with Spy++ to see if it is a program that does have a classname and use that. Otherwise you can just use null and keep track of the documents title.

So with your wordpad example try this line...

CODE

IntPtr hWnd = FindWindow(null, "Document - WordPad");


And that should work for you. Enjoy!

"At DIC we be program controlling code ninjas with the Win API!" decap.gif
User is online!Profile CardPM
+Quote Post

born2c0de
RE: How Do I Put Text In Another Program?
27 Dec, 2007 - 11:15 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
WordPad has a classname.
It's "WordPadClass".

I just called the GetClassName() Function and got the answer.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:39PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month