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;