This example describes how to simulate a common mouse operation in your own program.
Start a new project and add 4 TButton components to the blank form. The form after you add the component is shown in Figure 1.
Figure 1 form after adding a component
These 4 buttons are used to mimic the 4 actions of the mouse-moving the mouse to the specified position, left click, left-clicking, and Right-click.
The following is an example of how to simulate mouse action with the left mouse button:
procedure TForm1.btnLClickClick(Sender: TObject);
begin
SetCursorPos(10,10);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
end;
First you need to set the coordinates you want to click by using the Setcursorpos function. The left mouse button is then pressed through the mouse_event (mouseeventf_leftdown,0,0,0,0) statement at (10,10) coordinates. Finally, don't forget to release the left mouse button through the mouse_event (mouseeventf_leftup,0,0,0,0) statement.
The operation of the double-click can be achieved by two consecutive clicks, which can be implemented by specifying the 1th parameter of the Mouse_event function Mouseeventf_move, the 2nd and 3rd parameters to the left, and the right click and left-click Principle, except that the Mouse_ The arguments in the event function are replaced with Mouseeventf_rightdown and Mouseeventf_rightup, respectively.
The program code is as follows:
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
TForm1 = Class (Tform)
Btnmove:tbutton;
Btnlclick:tbutton;
Btnldclick:tbutton;
Btnrclick:tbutton;
Procedure Btnmoveclick (Sender:tobject);
Procedure Btnlclickclick (Sender:tobject);
Procedure Btnldclickclick (Sender:tobject);
Procedure Btnrclickclick (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.btnmoveclick (Sender:tobject);
Begin
Mouse_event (mouseeventf_move,100,100,0,0);
End
Procedure Tform1.btnlclickclick (Sender:tobject);
Begin
Setcursorpos (10,10);
Mouse_event (mouseeventf_leftdown,0,0,0,0);
Mouse_event (mouseeventf_leftup,0,0,0,0);
End
Procedure Tform1.btnldclickclick (Sender:tobject);
Begin
Setcursorpos (200,200);
Mouse_event (mouseeventf_leftdown,0,0,0,0);
Mouse_event (mouseeventf_leftup,0,0,0,0);
Mouse_event (mouseeventf_leftdown,0,0,0,0);
Mouse_event (mouseeventf_leftup,0,0,0,0);
End
Procedure Tform1.btnrclickclick (Sender:tobject);
Begin
Setcursorpos (200,200);
Mouse_event (mouseeventf_rightdown,0,0,0,0);
Mouse_event (mouseeventf_rightup,0,0,0,0);
End
End.
Save the file, and then press F9 to run the program, the initial picture of the program running as shown in Figure 2.
Figure 2 Initial screen of program operation
You can complete the simulation move mouse to the specified position, left click, left-clicking, and Right-click by clicking Move Mouse, left click, and left-click button on the form.
This program describes how to simulate the mouse in the program commonly used operations, which is very important in remote monitoring software, through the Mouse_event function can achieve remote mouse management and operation.