B4J Question Mouse Position on Canvas

ElliotHC

Active Member
Licensed User
How do you get the mouse position relative to the corner of a Canvas?

I may move the app around and I need the mouse position to be relative to what is drawn on the canvas. Or do I have to deduct the form position from the mouse position?

Any ideas or code examples of this being done?

Thanks
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
The mouse position returned is relative to the view that owns the event handler, so for "view1" ...
B4X:
Sub view1_MouseClicked (EventData As MouseEvent)
   . . . .
End Sub
If your canvas has been allocated to "view1" then the EventData coordinates are relative to the top corner of the view and therefore to that corner of the canvas.

[Edit] Just to be complete, if the canvas is assigned to "view2" which is a child of view1 then the top-left coordinate of view2 will have to be subtracted from the EventData values to get the position on the canvas. Also if the canvas is assigned to a scrollable pane then the scroll-offsets of the pane will have to be added to get the canvas position.
 
Last edited:
Upvote 0
Top