Solved

pressing buttons

Posted on 2000-03-19
13
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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