在 Delphi 中如何在窗体上的某个坐标点点击鼠标?

wnavrhmk  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(185)

在 Delphi 中如何在窗体上的某个坐标点点击鼠标?我如何理解首先需要得到的屏幕坐标?

ndasle7k

ndasle7k1#

如果窗体在你的应用程序里面,那就相当简单了:

var x , y : integer;
      x:= 100;
      y := 100;
      var ClientAreaPos := form3.ClientToScreen(Point(0,0));
      x := x + ClientAreaPos.X;
      y := y + ClientAreaPos.y;
      SetCursorPos(x, y); //set cursor to Start menu coordinates
      mouse_event(MOUSEEVENTF_LEFTDOWN,0, 0, 0, 0); //press left button
      mouse_event(MOUSEEVENTF_LEFTUP,0, 0, 0, 0); //release left button

如果没有-那么你需要通过WinApi获得表格的坐标:

function ClickOnWindow(const ATargetWindowClass : PWideChar; ClientX, ClientY : integer) : boolean;
begin
  Result := false;
  var xTargetHWnd := 0;
  xTargetHWnd := FindWindow(ATargetWindowClass, nil); //try to find our window by WindowClassName
  if xTargetHWnd <> 0 then begin
    var xWindowRect := TRect.Empty;
    var xPoint := tpoint.Create(ClientX, ClientY);
    if ClientToScreen(xTargetHWnd, xPoint) then begin   //transform ClientPos to ScreenPos
      SetCursorPos(xPoint.X, xPoint.Y); //set mouse specified coordinates
      mouse_event(MOUSEEVENTF_LEFTDOWN,0, 0, 0, 0); //press left button
      mouse_event(MOUSEEVENTF_LEFTUP,0, 0, 0, 0); //release left button
      Result := true;
    end{if};
  end{if};
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  if ClickOnWindow('TForm3', 100, 100) then
    showmessage('Success')
  else
    showmessage('Fail');
end;

相关问题