单击控件时如何获取鼠标的坐标?

时间:2021-04-16 21:20:58

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.

在TImage的OnClick事件中,我想提取鼠标的x,y坐标。我更喜欢它们与图像有关,但与形式或窗口相关也同样好。

4 个解决方案

#1


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.

Mouse.CursorPos包含TPoint,TPoint又包含X和Y位置。此值位于全局坐标中,因此您可以使用ScreenToClient例程将屏幕坐标转换为窗口坐标,从而转换为表单。

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

根据Delphi帮助文件,Windows.GetCursorPos可能会失败,Mouse.CursorPos会将其包装起来,以便在失败时引发EOsException。

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;

#2


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.

Mouse.CursorPos属性将告诉您鼠标的当前位置。如果计算机运行缓慢,或者程序响应消息的速度很慢,则可能与OnClick事件首次发生时鼠标的位置不同。要在单击鼠标按钮时获取鼠标的位置,请使用GetMessagePos。它报告屏幕坐标;使用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.

另一种方法是自己处理OnMouseDown和OnMouseUp事件;他们的参数包括坐标。请记住,两个事件都需要发生才能发生点击。您可能还想检测拖动操作,因为您可能不希望将拖动视为点击。

#3


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.

正如其他人所说,你可以使用Mouse.CursorPos或GetCursorPos函数,但你也可以只处理OnMouseDown或OnMouseUp事件而不是OnClick。这样,您可以将X和Y值作为参数添加到事件处理程序中,而无需进行任何额外的函数调用。

#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 :)

在网上找到这个,并将其保存在我的代码片段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!

#1


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.

Mouse.CursorPos包含TPoint,TPoint又包含X和Y位置。此值位于全局坐标中,因此您可以使用ScreenToClient例程将屏幕坐标转换为窗口坐标,从而转换为表单。

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

根据Delphi帮助文件,Windows.GetCursorPos可能会失败,Mouse.CursorPos会将其包装起来,以便在失败时引发EOsException。

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;

#2


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.

Mouse.CursorPos属性将告诉您鼠标的当前位置。如果计算机运行缓慢,或者程序响应消息的速度很慢,则可能与OnClick事件首次发生时鼠标的位置不同。要在单击鼠标按钮时获取鼠标的位置,请使用GetMessagePos。它报告屏幕坐标;使用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.

另一种方法是自己处理OnMouseDown和OnMouseUp事件;他们的参数包括坐标。请记住,两个事件都需要发生才能发生点击。您可能还想检测拖动操作,因为您可能不希望将拖动视为点击。

#3


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.

正如其他人所说,你可以使用Mouse.CursorPos或GetCursorPos函数,但你也可以只处理OnMouseDown或OnMouseUp事件而不是OnClick。这样,您可以将X和Y值作为参数添加到事件处理程序中,而无需进行任何额外的函数调用。

#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 :)

在网上找到这个,并将其保存在我的代码片段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!