unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormClick(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //方法一: 使用 OnMouseDown 事件的参数: procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin //参数中的 X,Y 就是当前鼠标所在位置的坐标 //譬如显示看看: Text := Format('OnMouseDown: x=%d; y=%d', [X,Y]); end; //方法二: 使用 OnMouseMove 事件的参数: procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin //参数中的 X,Y 就是当前鼠标所在位置的坐标 //譬如显示看看: Text := Format('OnMouseMove: x=%d; y=%d', [X,Y]); end; //方法三: 使用 OnMouseUp 事件的参数: procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin //参数中的 X,Y 就是当前鼠标所在位置的坐标 //譬如显示看看: ShowMessage(Format('OnMouseUp: x=%d; y=%d', [X,Y])); end; //方法四: 使用 API 函数 GetCursorPos: procedure TForm1.FormClick(Sender: TObject); var pt: TPoint; begin GetCursorPos(pt); {这是获取的相对于屏幕的坐标} pt := ScreenToClient(pt); {转换成本地坐标} ShowMessage(Format('API 函数 GetCursorPos: x=%d; y=%d', [pt.X, pt.Y])); end; end.
http://www.delphitop.com/html/shubiao/1368.html
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.