Link to home
Start Free TrialLog in
Avatar of feup
feup

asked on

change component text

How can I change a component text?

For example, I have the search window from wordpad, and, from my application, I want to write there "test".

I can find the handler using a script tool named "FLUTE" (http://www.wtitle.demon.co.uk/flute.htm), but I couldn't do that in delphi.


Any suggestion?
Avatar of edey
edey

If you know how to get the handle, then look up the WM_SETTEXT message

GL
Mike
i have example of this unless you want to put one edey :-)
why I'd be happy too :)
In it's simplest form:

sendMessage(edit1.handle,WM_SETTEXT,0,integer(pchar('Edit Cleared!')));

will set the text of edit1 to 'Edit Cleared!', Hope this helps,

GL
Mike
Avatar of feup

ASKER

Thanks everyone, but I know the handler in my computer, but if I restart, I have to change it again, and that's not good.

Any idea?


thanks
you are not using correct handle for this type of operation.you must use the windows class name using findwindow() and findwindowex() functions.

WordpadClass is the classname of  wordpads mainwindow,and RichEdit20A is the classname of the edit that holds wordpads text.so you should be doing  like open a instance of wordpad then do this:

Procedure WriteToWordpad(what:string);
var h:hwnd;
begin
h:=FindWindow('WordpadClass',nil);
h:=FindWindowex(h,0,'RichEdit20A',nil);
if h<>0 then SendMessage(h,wm_setText,0,Integer(what))
        else showmessage('Error writing to notepad!');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
WritetoWordpad('some text here');
end;

Regards Barry
Hmmm...
I am not sure what you're asking, could you, perhaps, give another go at explaining?


GL
Mike
hi edey,
ya know if you use the apps handle (ie some numeral handle like 234233) this is just the handle windows assigns a control but it changes everytime the program or windows is re-started so tat is why to use classnames instead of window handles..
did that make sense :-)
sorry ~ the inthe, my previous comment was directed towards feup's last comment, I just wasn't fast enough :).  Though, given your comment, I guess fuep didn't have a good way of finding the handle, so your comment is very appropriate.

GL
Mike
Avatar of feup

ASKER

ok people, thanks for your comments. I'll try the examples, and then, I'll tell you something.


regards
Avatar of feup

ASKER

OK. That worked, but there are some problems :

- Some applications have their controls with the same name : Edit; Edit; Static; etc.. they are not numbered. How can I distinguish them?

- What abou if I have 2 windows of wordpad oppened. How can I choose between them?

- This is a little diffrent from my question, but I need it too (How can I click their button?)


Thanks
the apps may have controls with same name but the order to get to them will be different because the parent(first) call is never going to be same.

ie:
h:=FindWindow('WordpadClass',nil);
h:=FindWindowex(h,0,'RichEdit20A',nil);

h:=FindWindow('grandpa_class',nil);
h:=FindWindow(h,0,'dad_class',nil);  
h:=FindWindowex(h,0,'RichEdit20A',nil);

if there are two windows open it will pick first window it finds which will be top one in z-order(the last one that had focus ).

to click a button find the window(class name as before)of it using findwindow/findwindowex then send  :

sendmessage(h,WM_LBUTTONDOWN,0,0);     sendmessage(h,WM_LBUTTONUP,0,0);

Regards Barry
Avatar of feup

ASKER

Thanks barry, but I couldn't understand.

Imagine I have a window with the classname 'WINDOW', and two edit boxes in a row : 'edit' and 'edit'.

How can I write 'Hello' in the first one and 'World' in the second.


By the way : I've found out that a program I'm now using to detect the classes of the components, and it shows the index, but not for all the components. Does it help?


regards
what is the name of the program you are using to find classnames?and what is the program with two classes named "edit"
do you have Winsight?(comes with professional and c/s versions of delphi)
another one i like to use for program info is called sinfo.
http://212.187.198.142/software/free/utilities/win95/sw42.html

i never came accross a situation where there would be 2 class names the same on same control(usually always slightly different parent etc)and wondered if you were getting correct information.
i will make an example to put text into wordpads find edit and press find button.
back later.
note using the findwindow fiunctions you can also passthe window caption.
the find dialog in wordpad for instance has a classname of #32770 and a window caprion of "find" ,so we can call:
h:=FindWindow('#32770','Find')
or leave one of the parameters as 0;
here is an example ,start wordpad with a document then call this:

Procedure Find_in_Wordpad;
var
 h,he,hc:hwnd;
   s : string;
  b : boolean;
 begin
b:=InputQuery('CustomSearchBox','Enter a word to search for...', s); //get a search feild
if s = '' then
 showmessage('no text')   //ends proc if no text entered
 else begin
h:=FindWindow('WordpadClass',nil); //find wordpad instance
if h <> 0 then
setforegroundwindow(h)   //bring wordpad to fromt so we can use keybd_Event()
else showmessage('run wordpad');
SetFocus(h);
keybd_Event(VK_CONTROL,0,0,0);  //ctrl - f is the find dialogs shortcut
keybd_Event(ord('F'),0,0,0);
keybd_Event(ord('F'),0,KEYEVENTF_KEYUP, 0);
keybd_Event(VK_CONTROL,0,KEYEVENTF_KEYUP, 0);
h := 0; //reset h;
repeat
h:=FindWindow('#32770','Find')//wait for find window to appear
until h <> 0;
he:=FindWindowex(h,0,'Button','&Find Next'); //find button
hc:=FindWindowex(h,0,'Button','Cancel');    //cancel button
h:=FindWindowex(h,0,'Edit',nil);           //text feild
if h<>0 then SendMessage(h,wm_setText,0,Integer(s))
        else showmessage('Error sending text!');
sendmessage(he,WM_LBUTTONDOWN,0,0);
sendmessage(he,WM_LBUTTONUP,0,0);     //do the find and close dialog
sendmessage(hc,WM_LBUTTONDOWN,0,0);
sendmessage(hc,WM_LBUTTONUP,0,0);
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Find_in_Wordpad;
end;

if you have a specific program that you are trying to do this stuff in let me know i may have it too and be able to help more.
Regards Barry
Avatar of feup

ASKER

Hi!

The application I'm trying to use is "SPY" from Wintask ... www.wintask.com

and the application that hat the two edit boxes class named "edit" is "Spedia" ... www.spedia.net


I wish to make an automated way to make the login.


Thanks

Note : In your example, the find dialog has only one edit box, so it doesn't solve my problem
hi,
ok got it ,we have to first use findwindow() to get first "edit" then use getnextwindow() to get the second(password edit),here is tested example:


Procedure Speedia_Login;
var
 h,h1,h2,he:hwnd;
s1,s2:string;
begin
s1 := 'username';
s2 := 'somepassword';
h:=FindWindow('#32770','Spedia - Authentication');
he := FindWindowex(h,0,'Button','&Login');
h1:=FindWindowex(h,0,'Edit',nil);
if h1<>0 then SendMessage(h1,wm_setText,0,Integer(s1))
        else showmessage('Error writing username');
h2:=getnextWindow(h1,GW_HWNDNEXT);
if h2<>0 then SendMessage(h2,wm_setText,0,Integer(s2))
        else showmessage('Error writing password');
sendmessage(he,WM_LBUTTONDOWN,0,0);
sendmessage(he,WM_LBUTTONUP,0,0);     //do the find and close dialog
sendmessage(he,WM_LBUTTONDOWN,0,0);
sendmessage(he,WM_LBUTTONUP,0,0);
 end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Speedia_Login;
end;


hope that what you needed
Regards Barry
Avatar of feup

ASKER

Great. Thanks.

Could you please help me on a question you've answerd before, because I'm having a little problem?

https://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10248589 

Make an answer on this one to give you the points.


Thanks
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

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 feup

ASKER

Please, check the other question, because is very important.


regards