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.
相关问题
- Is there a Delphi 5 component that can handle .png
- Is there a way to install Delphi 2010 on Windows 2
- Is TWebBrowser dependant on IE version?
- iOS objective-c object: When to use release and wh
- DBGrid - How to set an individual background color
相关文章
- Best way to implement MVVM bindings (View <-> V
- Windows EventLog: How fast are operations with it?
- How to force Delphi compiler to display all hints
- Coloring cell background on firemonkey stringgrid
- HelpInsight documentation in Delphi 2007
- Can RTTI interrogate types from project code at de
- What specifically causes EPrivilege to be raised?
- Equivalent to designer guidelines in code
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.
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.
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.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!