Get X,Y coordinates from inside the DrawingArea

Post

Posted
Rating:
#1 (In Topic #1235)
Trainee
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:

Code (gambas)

  1. Public Sub DrawingArea1_MouseDown()
  2.  
  3.   Dim da As DrawingArea = FMain.Controls["DrawingArea1"]
  4.  
  5.   Print da.Cursor.X & "," & da.Cursor.Y
  6.  
  7.  

However, I instead get the error:
Null object

Is what I'm after possible?  If yes, how?
Online now: No Back to the top

Post

Posted
Rating:
#2
Trainee
Nevermind.  I figured it out.

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

Code (gambas)

  1. Public Sub DrawingArea1_MouseDown()
  2.  
  3.   Print Mouse.X & "," & Mouse.Y
  4.  
  5.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’

ak2766 said

Nevermind.  I figured it out.

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

Code (gambas)

  1. Public Sub DrawingArea1_MouseDown()
  2.  
  3.   Print Mouse.X & "," & Mouse.Y
  4.  
  5.  

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.

Code (gambas)

  1. Public Sub GetControlMousePos(Ctrl As Control) As Integer[]
  2.  
  3.  Return [Mouse.ScreenX - Ctrl.ScreenX, Mouse.ScreenY - Ctrl.ScreenY]
  4.  
  5.  

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
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Ps.
I know you didn't need it but in that first example it would be easier to do this…

Code (gambas)

  1. Public Sub DrawingArea1_MouseDown()
  2.  
  3.  
  4.  

In any Event the Last keyword points to the calling object.
so in a drawingarea mousedown event Last is the drawingarea
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
Thanks for the extra pointers, Bruce.

Really getting to know GAMBAS now -  :D !
Online now: No Back to the top
1 guest and 0 members have just viewed this.