SetCursorPos出现故障?(SetCursorPos malfunctions?)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
SetCursorPos出现故障?(SetCursorPos malfunctions?)

我想在delphi中编写一个程序来模拟具有特定速度的移动鼠标指针(类似于AutoIT MouseMove函数)。 我的代码错误或者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;

由于某种原因,鼠标指针卡在某个X点,然后跳回到0,0但为什么会这样?

I wanted to write a procedure in delphi to simulate a moving mouse pointer with a specific speed (similar to the AutoIT MouseMove function). Either my code is wrong or SetCursorPos malfunctions after it gets called too many times. Here is the function I have:

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;

I use it like this:

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;

For some reason the mousepointer gets stuck at a certain X point and then jumps back to 0,0 but why is that?

最满意答案

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

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);

您还可以改进检查GetCursorPos和SetCursorPos函数结果的方法。

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

You code is stuck , because in the repeat loop, the condition

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

is never satisfied when you pass the values X=0 and Y=Screen.Height, so you must modify your loop to pass only valid screen coordinates values

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

Also you can improve your method checking the result of the GetCursorPos and SetCursorPos functions.

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

更多推荐

本文发布于:2023-04-20 16:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/367a01f5f827fb2bc06ed218169aecdc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现故障   SetCursorPos   malfunctions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!