问题描述
在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.
推荐答案
鼠标。 CursorPos包含TPoint,它又包含X和Y位置。该值位于全局坐标系中,因此您可以使用ScreenToClient例程将窗体坐标转换为窗口坐标。
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.
根据Delphi帮助文件,Windows.GetCursorPos可能会失败,如果Mouse.CursorPos失败,则会将其包装为引发EOsException。
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;
这篇关于单击控件时如何获取鼠标的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!