Solved

pressing buttons

Posted on 2000-03-19
13
248 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
Comment Utility
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
Comment Utility
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
Comment Utility
mmm.
wouldnt really call 2 lines over the top .. ;-)
0
 
LVL 7

Expert Comment

by:ahalya
Comment Utility
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
Comment Utility
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
Comment Utility
Are you asked about external application?

Cheers
Igor.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:X_Kalibur
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points from 30 to 50
0
 

Author Comment

by:frangers99
Comment Utility
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
Comment Utility
sorry to change the question a little but both solutions are excellent, thanks to everybody who contributed
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

8 Experts available now in Live!

Get 1:1 Help Now