Get X,Y coordinates from inside the DrawingArea

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
ak2766
Posts: 19
Joined: Sunday 3rd April 2022 4:59am
Location: Melbourne, Australia

Get X,Y coordinates from inside the DrawingArea

Post by ak2766 »

I've got dots in a DrawingArea and I'm looking to click on any of these dots and get the x,y coordinates.

I tried using the `DrawingArea_MouseDown` event:

Public Sub DrawingArea1_MouseDown()

  Dim da As DrawingArea = FMain.Controls["DrawingArea1"]

  Print da.Cursor.X & "," & da.Cursor.Y

End


However, I instead get the error:
Null object
Is what I'm after possible? If yes, how?
ak2766
Posts: 19
Joined: Sunday 3rd April 2022 4:59am
Location: Melbourne, Australia

Re: Get X,Y coordinates from inside the DrawingArea

Post by ak2766 »

Nevermind. I figured it out.

No need to use DrawingArea object. Just need to get Mouse.X, Mouse.Y

Public Sub DrawingArea1_MouseDown()

  Print Mouse.X & "," & Mouse.Y
 
End
User avatar
BruceSteers
Posts: 1607
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Get X,Y coordinates from inside the DrawingArea

Post by BruceSteers »

ak2766 wrote: Tuesday 30th April 2024 8:36am Nevermind. I figured it out.

No need to use DrawingArea object. Just need to get Mouse.X, Mouse.Y

Public Sub DrawingArea1_MouseDown()

  Print Mouse.X & "," & Mouse.Y
 
End
You got it.

for any control Mouse event like MouseDown / MouseMove, Mouse.X and Mouse.Y are relative to the control contents (0,0 being top left)

From outside of a controls mouse event you will find Mouse.X and Mouse.Y give a "no mouse data" error but then you can use the screen alternatives.
Public Sub GetControlMousePos(Ctrl As Control) As Integer[]

 Return [Mouse.ScreenX - Ctrl.ScreenX, Mouse.ScreenY - Ctrl.ScreenY]

End


Control.Cursor is for setting the cursor image
Ie.
DrawingArea1.Cursor = Cursor.CrossHair will make the cursor be a crosshair
DrawingArea1.Cursor = Cursor.Default will use the default
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1607
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Get X,Y coordinates from inside the DrawingArea

Post by BruceSteers »

Ps.
I know you didn't need it but in that first example it would be easier to do this...

Public Sub DrawingArea1_MouseDown()

  Dim da As DrawingArea = Last

End


In any Event the Last keyword points to the calling object.
so in a drawingarea mousedown event Last is the drawingarea
If at first you don't succeed , try doing something differently.
BruceS
ak2766
Posts: 19
Joined: Sunday 3rd April 2022 4:59am
Location: Melbourne, Australia

Re: Get X,Y coordinates from inside the DrawingArea

Post by ak2766 »

Thanks for the extra pointers, Bruce.

Really getting to know GAMBAS now - :D !
Post Reply