Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pressing buttons

Posted on 2000-03-19
13
Medium Priority
?
259 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
Independent Software Vendors: 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 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