简体   繁体   中英

How to click the mouse at certain coordinates on the form in Delphi?

How to click the mouse at certain coordinates on the form in Delphi? How I understand first of all need to get coordinates of screen?

If form inside your application, it's quite simple:

    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

if not – then you need get coordinate of form by 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM