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

发布于 2024-07-19 09:02:34 字数 75 浏览 5 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

老子叫无熙 2024-07-26 09:02:34

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

根据 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 := Image1.ScreenToClient(pt);
  // now have Image position
  Label2.Caption := 'X = '+IntToStr(pt.x)+', Y = '+IntToStr(pt.y);
end;

注意:如果您在 ScreenToClient 方法中省略“Image1”,它将引用表单。

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.

紫瑟鸿黎 2024-07-26 09:02:34

Mouse.CursorPos 属性将告诉您鼠标的当前位置。 如果计算机运行缓慢,或者您的程序对消息的响应速度很慢,则它可能与 OnClick 事件首次发生时鼠标所在的位置不同。 要获取单击鼠标按钮时鼠标的位置,请使用 GetMessagePos。 它报告屏幕坐标; 使用 TImage.ScreenToClient 转换为客户端坐标。

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

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.

锦欢 2024-07-26 09:02:34

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

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.

倾`听者〃 2024-07-26 09:02:34

这个怎么样?

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;

在网上的某个地方发现了一次并将其保存在我的codesnippet DB中:)

此页面 可能会解决您所有的问题,但是...似乎有从客户端到屏幕坐标再返回的功能。

祝您好运!

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!

没︽人懂的悲伤 2024-07-26 09:02:34

至 Firemonkey (FMX):

var
  p: TPointF;
begin
  p := Screen.MousePos;
end;

To Firemonkey (FMX):

var
  p: TPointF;
begin
  p := Screen.MousePos;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文