如何转换像素坐标以用于MOUSEEVENTF_MOVE

时间:2016-09-23 06:00:31

标签: delphi mouseevent sendinput

我正在使用SendInput移动鼠标指针。根据MSDN使用坐标转换可以很好地发送绝对值。相对价值是另一回事。试验和错误表明大约0.625的因子给出了两个方向的正确结果。 如何计算必须用于相对坐标转换的因子? 我使用的代码如下所示:

procedure TForm1.BtAbsoluteClick(Sender: TObject);
var
  Inp: TInput;
  xp,yp: integer; //pixel co-ordinates
  xn,yn: integer; //normalised co-ordinates
begin
  xp := 500;
  yp := 200;
  {calculate normalised co-ordinates}
  xn := round(xp*65536/Screen.Width);
  yn := round(yp*65536/Screen.Height);
  {move mouse pointer}
  Inp.IType := INPUT_MOUSE;
  Inp.mi.dx := xn;
  Inp.mi.dy := yn;
  Inp.mi.mouseData := 0;
  Inp.mi.dwFlags := MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE;
  Inp.mi.time := 0;
  Inp.mi.dwExtraInfo := 0;
  SendInput(1, Inp, SizeOf(Inp));
end;

procedure TForm1.BtRelativClick(Sender: TObject);
var
  Inp: TInput;
  xp,yp: integer; //pixel co-ordinates
  xn,yn: integer; //normalised co-ordinates
begin
  xp := 500; //with no correction cursor moves to 800  >>  0.625
  yp := 200; //with no correction cursor moves to 318  >>  0.625
  {calculate normalised co-ordinates}
  xn := round(xp*0.625);
  yn := round(yp*0.625);
  {move mouse pointer}
  Inp.IType := INPUT_MOUSE;
  Inp.mi.dx := xn;
  Inp.mi.dy := yn;
  Inp.mi.mouseData := 0;
  Inp.mi.dwFlags := MOUSEEVENTF_MOVE;
  Inp.mi.time := 0;
  Inp.mi.dwExtraInfo := 0;
  SendInput(1, Inp, SizeOf(Inp));
end;

我正在使用在Win10上运行的Delphi 10

0 个答案:

没有答案