Author: Sebastian Volland
How can I simulate mouse clicks in my application written in Delphi?
Answer:
You can easily simulate mouse clicks or moves with the mouse_event-function. You
can find more information about the parameters and flags for this function in the
Delphi-helpfile.
This function can be useful when you can not control other applications by OLE or
something like that.
Example:
You want to start an application, and doubleclick on an item which is at
x,y-position 300,400 in this application. Put a TTimer on your form, set it to
Disabled and try this example code:
1 procedure TForm1.FormCreateOrWhatever;
2 begin
3 winexec('myexternalapplication.exe', sw_shownormal); // start app
4 timer1.interval := 2000; // give the app 2 secs to start up
5 timer1.enabled := true; // start the timer
6 end;
7
8 procedure TForm1.Timer1Timer(Sender: TObject);
9 var
10 point: TPoint; // point-structure needed by getcursorpos()
11 begin
12 getcursorpos(point); // get current mouse position
13 setcursorpos(300, 400); // set mouse cursor to menu item or whatever
14 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // click down
15 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // +click up = double click
16 setcursorpos(point.x, point.y); // set cursor pos to origin pos
17 timer1.enabled := false; // stop
18 end;
The timer is needed to give the application time to start up. Be sure you don't move the mouse while mouse_event is executed. That's it!
|