Spostare con il mouse un Controllo grafico generato in una DrawingArea

Da Gambas-it.org - Wikipedia.

Il seguente codice ci consente di creare e di spostare all'interno di una DrawingArea un proprio Oggetto grafico Figlio. [nota 1]

Private DrawingArea1 As DrawingArea
Private lb As Label
Private i As Integer
Private spx As Short
Private spy As Short


Public Sub _new()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.White
 End With

End

Public Sub DrawingArea1_MouseDown()

' Se non si è cliccato con il tasto sinistro del mouse, si esce dalla routine:
 If Not Mouse.Left Then Return

 Inc i
 
' Genera e imposta una "Label", assegnadola come "figlia" alla "DrawingArea":
 With lb = New Label(DrawingArea1) As "Etichetta"
   .X = Mouse.X
   .Background = Color.SoftOrange
   .Border = Border.Raised
   .Text = " Label " & CStr(i)
 End With
    
End 

Public Sub DrawingArea1_MouseMove()   
 
' Se non si è cliccato con il tasto sinistro del mouse, si esce dalla routine:
 If Not Mouse.Left Then Return

 DrawingArea1.Mouse = 18
 lb.X = Mouse.X
 lb.Y = Mouse.Y
  
End

Public Sub DrawingArea1_MouseUp()

' Se non si è cliccato con il tasto sinistro del mouse, si esce dalla routine:
 If Not Mouse.Left Then Return

 DrawingArea1.Mouse = Mouse.Default
 
 With lb
   .W = .Font.TextWidth(.Text) + 5
   .H = .Font.TextHeight(.Text)
   .Y = Mouse.Y - .H
 End With
   
End 

Public Sub Etichetta_MouseDown()
 
 spx = Mouse.X
 spy = Mouse.Y
 
End

Public Sub Etichetta_MouseMove()
 
 Last.Mouse = 18
 With Last
   .X = .X + Mouse.X - spx
   .Y = .Y + Mouse.Y - spy
 End With
 
End

Public Sub Etichetta_MouseUp()
 
 Last.Mouse = Mouse.Default
 
End


Note

[1] Vedere anche le seguenti pagine: