2013-01-14 24 views
3

我想在delphi中编写一个程序来模拟具有特定速度的移动鼠标指针(类似于AutoIT MouseMove函数)。 被调用太多次后,我的代码出错或SetCursorPos出现故障。 这里是我具备的功能:SetCursorPos故障?

procedure MoveMouse (X, Y, Speed : Integer); 
var 
    P  : TPoint; 
    NewX : Integer; 
    NewY : Integer; 
begin 
    if X < 0 then exit; 
    if Y < 0 then exit; 
    if X > Screen.Height then exit; 
    if Y > Screen.Width then Exit; 
    repeat 
    GetCursorPos(P); 
    NewX := P.X; 
    NewY := P.Y; 
    if P.X <> X then begin 
     if P.X > X then begin 
     NewX := P.X - 1; 
     end else begin 
     NewX := P.X + 1; 
     end; 
    end; 
    if P.Y <> Y then begin 
     if P.Y > Y then begin 
     NewY := P.Y - 1; 
     end else begin 
     NewY := P.Y + 1; 
     end; 
    end; 
    sleep (Speed); 
    SetCursorPos(NewX, NewY); 
    until (P.X = X) and (P.Y = Y); 
end; 

我用这样的:

procedure TForm1.btn1Click(Sender: TObject); 
var 
    X : Integer; 
    Y : Integer; 
begin 
    for X := 0 to Screen.Width do begin 
    for Y := 0 to Screen.Height do begin 
     MouseClick (X, Y, 1); 
    end; 
    end; 
end; 

出于某种原因的MousePointer卡在某X点,然后跳回0,0但为什么就是它?

回答

6

您的代码被卡住,因为在重复循环,条件

until (P.X = X) and (P.Y = Y); 

是永远不会满足,当你通过值X = 0,Y = Screen.Height,所以你必须修改你的循环传递唯一有效的屏幕坐标值

for X := 0 to Screen.Width-1 do 
    for Y := 0 to Screen.Height-1 do 
     MoveMouse (X, Y, 1); 

你也可以提高你的方法检查的GetCursorPosSetCursorPos函数的结果。

+0

就是这样!非常感谢! –