How do I get the coordinates of the mouse when a control is clicked?
Asked Answered
D

5

19

In a TImage's OnClick event, I would like to extract the x,y coordinates of the mouse. I would prefer them in relation to the image, but in relation to the form or window is just as good.

Domingadomingo answered 5/5, 2009 at 21:9 Comment(1)
This will be even more important for double clicks.Doorn
D
36

Mouse.CursorPos contains the TPoint, which in turn contains the X and Y position. This value is in global coordinates, so you can translate to your form by using the ScreenToClient routine which will translate screen coordinates to window coordinates.

According to the Delphi help file, Windows.GetCursorPos can fail, Mouse.CursorPos wraps this to raise an EOsException if it fails.

var
  pt : tPoint;
begin
  pt := Mouse.CursorPos; 
  // now have SCREEN position
  Label1.Caption := 'X = '+IntToStr(pt.x)+', Y = '+IntToStr(pt.y);
  pt := Image1.ScreenToClient(pt);
  // now have Image position
  Label2.Caption := 'X = '+IntToStr(pt.x)+', Y = '+IntToStr(pt.y);
end;

Note: if you leave out the "Image1" for the ScreenToClient method, it will refer to the form.

Dedededen answered 5/5, 2009 at 21:19 Comment(0)
S
11

The Mouse.CursorPos property will tell you the current position of the mouse. If the computer is running sluggishly, or if your program is slow to respond to messages, then it might not be the same as the position the mouse had when the OnClick event first occurred. To get the position of the mouse at the time the mouse button was clicked, use GetMessagePos. It reports screen coordinates; translate to client coordinates with TImage.ScreenToClient.

The alternative is to handle the OnMouseDown and OnMouseUp events yourself; their parameters include the coordinates. Remember that both events need to occur in order for a click to occur. You may also want to detect drag operations, since you probably wouldn't want to consider a drag to count as a click.

Slackjawed answered 6/5, 2009 at 13:30 Comment(1)
This answer makes it obvious that ScreenToClient is a method not just a "routine". If it's called without explicitly naming an object, this object is self (mostly some TForm). There are circumstances where another object makes more sense, like in this case. BTW you speak of drag - isn't it move (OnMouseMove)?Doorn
I
6

As others have said, you can use Mouse.CursorPos or the GetCursorPos function, but you can also just handle the OnMouseDown or OnMouseUp event instead of OnClick. This way you get your X and Y values as parameters to your event handler, without having to make any extra function calls.

Inflorescence answered 5/5, 2009 at 21:22 Comment(0)
B
4

How about this?

procedure TForm1.Button1Click(Sender: TObject);
var
MausPos: TPoint;
begin
  GetCursorPos(MausPos);
  label1.Caption := IntToStr(MausPos.x);
  label2.Caption := IntToStr(MausPos.y);
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  SetCursorPos(600, 600);
end;

Found this online somewhere once and saved it in my codesnippet DB :)

This page will probably solve all your questions however... There appear to be functions to go from client to screen coordinates and back etc..

Good luck!

Bandurria answered 5/5, 2009 at 21:18 Comment(0)
B
0

To Firemonkey (FMX):

var
  p: TPointF;
begin
  p := Screen.MousePos;
end;
Benzocaine answered 25/10, 2021 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.