Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 925
Arthur Avatar asked May 05 '09 21:05

Arthur


People also ask

How do I find my mouse click coordinates?

The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position. The x position of the event is found using the 'clientX' property. The x position of the canvas element, i.e. the left side of the rectangle can be found using the 'left' property.

Why is my mouse not clicking where pointer is?

Open control panel > Hardware & Sound. Click on Mouse. In mouse properties, click on pointer option tab. Decrease the motion of the mouse and check if it works fine.

What is cursor location?

The cursor position is represented by the line number and the character number and signifies where the next character will be displayed. For example, cursor position 1,1 always indicates the upper-leftmost corner position on the terminal. Cursor position 10,30 indicates the 30th character position on the 10th line.

How do I find my mouse coordinates on a Mac?

Answer: A: It does not use an AppleScript, Shell Script or anything else, but if you are just looking to get the coordinates of the mouse on the screen, +command, shift 4+ will give you the coordinates. Then press escape when you have them.


2 Answers

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 := ScreenToClient(pt);
  // now have FORM position
  Label2.Caption := 'X = '+IntToStr(pt.x)+', Y = '+IntToStr(pt.y);
end;
like image 187
skamradt Avatar answered Oct 13 '22 00:10

skamradt


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.

like image 29
Rob Kennedy Avatar answered Oct 13 '22 00:10

Rob Kennedy