Link to home
Start Free TrialLog in
Avatar of SuperMario
SuperMario

asked on

Program manipulation from a dialog

I am curious to know how to set edit box text to a string in another program's window. For example, the program I am working with only has one edit box. How would I go about putting text in that from another program? Please help.

Thanks
D
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
Hmm - i'd have sent a WM_SETTEXT to the edit window...
Avatar of nietod
nietod

I prefer using functions to messages where possible.  Provides type safety etc.
Avatar of SuperMario

ASKER

I know to use SetWindowText(), but the problem is I don't know how to get the other program's edit box ID. If I knew how to do that, I would be on my way. That's why I said there is only one edit box... because maybe that would make it easier?

D
What do you have to work with?  What do you know about this window?  Like do you have its parent window's handle?  its process ID?  do you have the parent window's title? etc?
Yes. The parent window is composed of no more than a CView window and an edit box. The parent window's title is The Palace. (The objective of this program is to give users a script library usable on some Palace servers).

D
Get the handle to the parent window using EnumWindows() or FindWindow()  (To use FindWindow, you need the window class name, you can obtain that with Spy++).  Then use EnumChildWindows() to go through all the child windows until you find one with the right class.  (Use GetClassName().)
Okay... Now I have this:
Handle: 00000D0C
I had the process ID but I just closed it on accident..

If this helps any..
More points go to you later on when I gain them.
D
I also got the class... Palace. Now I have the process ID, the handle, the class and just about everything.

What now?
Hey, my brain is slowly growing!

I used Spy++ to find the window stuff. I then added this into my code:

{
   CString input;
   GetDlgItem(IDC_EDIT1)->GetWindowText(input);
   FindWindow("Palace","The Palace")->SetWindowText(input);
   // I need to add a wildcard to "The Palace" somehow, because
   // it changes when you switch servers.
}

As I expected, it changed the caption of the Palace window to whatever the user typed in the edit box in my program.

Now I want to be able to change the caption of the Palace edit box to whatever the user typed in MY edit box.

That's the whole point behind this.
Thanks!
D

obtaining the process ID or handle at a particular time (like by using spy) is of no value--they change each time the program is run.  You must have code that can obtain them at any time.

Wildcards are not avaialble to FindWindow().  You must know the title exactly.  It sounds like you don't, so use EnumWindows() instead.  In this case you can check each window enumerated to make sure the title is "reasonable?" and that the window has the right class.  From there, you can start searching the child windows.
Okay...

but how do I set that window's edit box text?
Is it like,
EnumWindows("Palace")->GetDlgItem(IDC_EDITBOX)->SetWindowText("blah");
?
D
I see, I missunderstood. I read

"how to set edit box text to a string in another program's window."
as how to set your edit box to [a string in another program's window].

Yes, that code will work if you now the ID to pass to GetDlgItem.  Otherwise, you need to enumerate the child windows and look for the edit box.  use EnumChildWindows() for that.  (I assume by -> you mean program logic, not the C++ operator, right?)

However, it is somewhat rude, if not risky to alter information in another program.  You will probably be okay in this case, but there is no rule that says that a program has to run propperly when you start tinkering with its windows.

Al less risky possiblity might be to send a past command to the program.  (if paste works in this case.)  This is less "hack" like.  but only works if the program responds to paste and if the edit box is empty or if the text there is selected.