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;
Y:=600;
Mouse_Event(MOUSEEVENTF_LEFTDOWN+MOUSEEVENTF_ABSOLUTE,X,Y,0,0);
Mouse_Event(MOUSEEVENTF_LEFTUP+MOUSEEVENTF_ABSOLUTE,X,Y,0,0);好象根本没有点屏上800*600的那个位置?
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.
SetForegroundWindow(Nhwnd);
// X:=800;
X:=trunc(65535/1280*700);
// Y:=600;
Y:=trunc(65535/1280*400); Mouse_Event(MOUSEEVENTF_LEFTDOWN+MOUSEEVENTF_ABSOLUTE,X,Y,0,0);
Mouse_Event(MOUSEEVENTF_LEFTUP+MOUSEEVENTF_ABSOLUTE,X,Y,0,0);上面是我的代码,为什么我点击了执行这段代码的Button,并没有点击那个句柄为Nhwnd的IE窗口中的点,而是执行这段代码的Button不停的闪,好象是在点这个BUTTON?