Solved

pressing buttons

Posted on 2000-03-19
13
249 Views
Last Modified: 2010-04-04
Is there a simple piece of code that enables you to press buttons on external applications. I have looked around and found different solutions, but none seem to work. So any ideas?

I currently use findwindow to get the window handle. the findwindowex to get the button handle and i have tried
sendmessage(handle,WM_KEYDOWN,13,0) then key up but this doesn't work
0
Comment
Question by:frangers99
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2633965
hi,
you can do this to press the buuton where handle is its handle gotten via findwindow:

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

(that is telling the buuton the mouse clicked it)
Regards Barry
0
 
LVL 1

Expert Comment

by:X_Kalibur
ID: 2634469
howdy,

actually, inthe's answer is a tad over the top....i'm doing the exact some thing for a program i'm working on right now..
this will do the exact same thing as inthe's answer, but it does it in one line:

sendmessage(YOUR_HANDLE, BM_CLICK, 0, 0);

hth,

x
0
 
LVL 17

Expert Comment

by:inthe
ID: 2634823
mmm.
wouldnt really call 2 lines over the top .. ;-)
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2634939
I didn't know that
sendmessage(YOUR_HANDLE, BM_CLICK, 0, 0);  would work.

I have been using
PostMessage( WindowHandle,
                      wm_Command,
                      DlgID, {of Button},
                      MakeLong(GetDlgItem(WHandle, DlgID), BN_Clicked));
all these days.
0
 

Author Comment

by:frangers99
ID: 2635065
Ok you're both right, but what do you do if the window that's popping up is showmodal (ie, the button has to be pressed before the next instruction of your code is run) what happens then? I tried both pieces of code and they don't work with showmodal forms (such as dialog boxes).

Thanks both of you for your help. But this final helper will have me set.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2635151
Are you asked about external application?

Cheers
Igor.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:X_Kalibur
ID: 2635656
ooh..

this is interesting...i have never tried to automatically press dialog buttons...

there are several programs which do this (eg: Buzz Orf)

is there a "GetDialogWindow" APi call?? ;-P~

-x

0
 
LVL 7

Expert Comment

by:ahalya
ID: 2636400
well, you can use a timer to do that. e.g.

Timer1.Interval := Whatever;
Timer1.Enabled := true;
YourForm.ShowModal;
....


//and in your Timer1.Timer event
Timer1.Enabled := false;
WindowHandle := FindWindow(ClassName, Caption);
BtnHandle := GetDlgItem(WHandle, DlgID);

PostMessage( WindowHandle, wm_Command,  DlgID,
                        MakeLong(BtnHandle , BN_Clicked));

0
 
LVL 2

Expert Comment

by:alexstewart@beta
ID: 2637903
You might try this function
....
mouse_event
....
my reading of the help file suggests this will do what you want. (use the absolute coords)
there is also a
keybd_event
if you are pressing defaul buttons this will work too.
The target app should be set active.

Alex
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2638017
or this:

if the button has a caption of 'Yes' with the Y has a underline for instance
it is '&Yes' (maybe you know ..
anyway you can do this:

var WName : Array[0..99] of Char;
    HH : THandle;

//Search Childs
Function TWCallBackChild(H : THandle; V : Longint) : Bool; stdcall;
begin
  GetwindowText(H,@WName[0],100);
  if StrPas(@WName[0]) = '&Yes' then
    HH := H;
  Result := True;
end;

//search Parents
Function TWCallBack(H : THandle; V : Longint) : Bool; stdcall;
begin
  GetwindowText(H,@WName[0],100);
  if StrPas(@WName[0]) = '&Yes' then
  begin
    HH := H;
  end
  else
    EnumChildWindows(H,@TWCallBackChild,V);
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Dummy : Longint;
begin
  HH := 0;
  EnumWindows(@TWCallBack,Dummy);  //Search through the windows
  if HH <> 0 then  //if found then press
  begin
    setforegroundwindow(hh);
sendmessage(HH,bm_click,0,0);//x_calibar
  end;
end;

end.

an example is open notepad and type something thenpress the x in corner to close notepad it asks to save "yes" "no" "cancel" buttons..
run the code above and it will press the '&Yes' button ..
0
 

Author Comment

by:frangers99
ID: 2641842
Adjusted points from 30 to 50
0
 

Author Comment

by:frangers99
ID: 2641843
Barry I am going to try your answer sorry for the showmodal trouble but i'm gonna boost the points up 20 and i'll try this code this arvo and if it's right, which i'm sure it will be then you got yourself the points
0
 

Author Comment

by:frangers99
ID: 2646496
sorry to change the question a little but both solutions are excellent, thanks to everybody who contributed
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now