delphi中模拟鼠标点击事件


本文整理自网络,侵删。

 delphi中模拟鼠标点击事件 

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Button1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Panel1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.lines.Add('mouse:left click me');
end;

procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbLeft then
//按下的是鼠标左键
begin
memo1.Lines.Add('mouse on button:left down at '+inttostr(x)+':'+inttostr(y));
end;
if button=mbRight then
//按下的是鼠标右键
begin
memo1.Lines.Add('mouse on button:right down at '+inttostr(x)+':'+inttostr(y));
end;
end;

procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbLeft then
//弹起的是鼠标左键
begin
memo1.Lines.Add('mouse on button:left up at '+inttostr(x)+':'+inttostr(y));
end;
if button=mbRight then
//弹起的是鼠标右键
begin
memo1.Lines.Add('mouse on button:right up at '+inttostr(x)+':'+inttostr(y));
end;
end;

procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
memo1.Lines.Add('mouse on button:move to '+inttostr(x)+':'+inttostr(y));
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
point:TPoint;
rnd:real;
begin
randomize;
point.x:=button1.left+3;
point.y:=button1.Top+3;
windows.ClientToScreen(form1.handle,point);
//在两个坐标系中转换坐标
Application.BringToFront;
//将程序放到前台,确保不会因发生误动作而执行了其它程序
setcursorpos(point.x,point.y);
//将光标移动到特定点
rnd:=random(6);
//产生随机数,后面的随机动作以此随机数为依据
case trunc(rnd) of
//根据产生随机数的不同而产生不同的鼠标动作
0:
begin
mouse_event(MOUSEEVENTF_LEFTUP,point.x,point.y,0,0);
//在点point处产生鼠标左键弹起动作
end;
1:
begin
mouse_event(MOUSEEVENTF_LEFTDOWN,point.x,point.y,0,0);
//在点point处产生鼠标左键按下动作
end;
2:
begin
mouse_event(MOUSEEVENTF_RIGHTUP,point.x,point.y,0,0);
//在点point处产生鼠标右键弹起动作
end;
3:
begin
mouse_event(MOUSEEVENTF_RIGHTDOWN,point.x,point.y,0,0);
//在点point处产生鼠标右键按下动作
end;
4:
begin
mouse_event(MOUSEEVENTF_LEFTDOWN,point.x,point.y,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,point.x,point.y,0,0);
//在点point处产生鼠标左键单击动作(单击实质就是鼠标先按下后弹起)
end;
5:
begin
mouse_event(MOUSEEVENTF_MOVE,trunc(rnd),trunc(rnd),0,0);
//在点point处产生鼠标移动动作
end;
end;// end case
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
timer1.Enabled:=false; //结束鼠标事件的产生
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Enabled:=true;
end;


end.

相关阅读 >>

Delphi win32_diskdrive 硬盘 参数说明

Delphi 下载一个jpg图片保存为bmp图片

Delphi释放内存一般这么写

tsqlconnection连接mysql(utf8)的问题

Delphi firedac,ado性能测试

Delphi webbrowser1提取网页中的所有链接、点击第 n 个链接

Delphi 从资源文件中加载字符

Delphi控件安装与删除

Delphi 过滤开头 结尾 全部 空格的函数

Delphi win10下message无法接收的问题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...