单击控件时如何获取鼠标坐标?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Mouse.CursorPos 包含 TPoint,TPoint 又包含 X 和 Y 位置。 该值采用全局坐标,因此您可以使用 ScreenToClient 例程将屏幕坐标转换为窗口坐标来转换为表单。
根据 Delphi 帮助文件,Windows.GetCursorPos 可能会失败,Mouse.CursorPos 会包装它以在失败时引发 EOsException。
注意:如果您在 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.
Note: if you leave out the "Image1" for the ScreenToClient method, it will refer to the form.
Mouse.CursorPos
属性将告诉您鼠标的当前位置。 如果计算机运行缓慢,或者您的程序对消息的响应速度很慢,则它可能与OnClick
事件首次发生时鼠标所在的位置不同。 要获取单击鼠标按钮时鼠标的位置,请使用GetMessagePos
。 它报告屏幕坐标; 使用TImage.ScreenToClient
转换为客户端坐标。另一种方法是自己处理
OnMouseDown
和OnMouseUp
事件; 它们的参数包括坐标。 请记住,两个事件都需要发生才能发生点击。 您可能还想检测拖动操作,因为您可能不希望将拖动视为单击。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 theOnClick
event first occurred. To get the position of the mouse at the time the mouse button was clicked, useGetMessagePos
. It reports screen coordinates; translate to client coordinates withTImage.ScreenToClient
.The alternative is to handle the
OnMouseDown
andOnMouseUp
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.正如其他人所说,您可以使用 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.
这个怎么样?
在网上的某个地方发现了一次并将其保存在我的codesnippet DB中:)
此页面 可能会解决您所有的问题,但是...似乎有从客户端到屏幕坐标再返回的功能。
祝您好运!
How about this?
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!
至 Firemonkey (FMX):
To Firemonkey (FMX):