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?
Asked
Active
Viewed 369 times
-2
-
2This sounds like a possible [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). *Why* do you want to send clicks to the Form? What are you trying to accomplish, exactly? There might be a better way. – Remy Lebeau Jun 22 '22 at 23:54
-
UIAutomation is the way to automate other programs – David Heffernan Jun 23 '22 at 07:32
-
I use TChromium and want to click JS element – Николай Агеев Jun 23 '22 at 08:18
-
@НиколайАгеев: Not trying to be rude or anything, but why didn't you include that information in your question from the beginning? – Andreas Rejbrand Jun 23 '22 at 08:25
-
This is my first post on this site, so I don't understand all the rules yet. I apologize – Николай Агеев Jun 23 '22 at 08:39
-
It's about common sense, not rules. [Clicking in a webpage should be done thru JavaScript using DOM](https://stackoverflow.com/q/67046826/4299358) because then you're unbound to scroll position, rendering settings and zoom of a displayed webpage. – AmigoJack Jun 23 '22 at 09:57
-
Also, Chrome based browsers do support automation in various ways. – David Heffernan Jun 23 '22 at 13:22
1 Answers
2
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;

Andreas Rejbrand
- 105,602
- 8
- 282
- 384

Softacom
- 613
- 2
- 6
-
double var in you code? As a result I have an error: E2076 This form of method call only allowed for class methods (Delphi) on var ClientAreaPos := – Николай Агеев Jun 23 '22 at 12:43
-
1Don't use mouse_event, you run the risk of interleaving with real events. Use SendInput. As is clearly stated in the documentation. – David Heffernan Jun 23 '22 at 13:21
-
1Inline variable declaration allowed from Delphi 10.3 and higher version. If you use older – just remove keyword VAR from code and put variable declaration before BEGIN as usual. – Softacom Jun 23 '22 at 13:27
-
@Softacom Sure, but then people ask for the datatype they should use - that knowledge/sense/understanding will get extinct shortly. – AmigoJack Jun 24 '22 at 10:09