Spostare con il mouse un rettangolo in una DrawingArea

Da Gambas-it.org - Wikipedia.

Nel seguente esempio si potrà spostare con il mouse sulla superficie di una DrawingArea un rettangolo (o un quadrato) precedentemente disegnato. [nota 1]
In particolare durante lo spostamento del rettangolo il puntatore del mouse resterà laddove si è cliccato all'interno del rettangolo.

Private DrawingArea1 As DrawingArea
Private x As Short
Private y As Short
Private Const W As Short = 200
Private Const H As Short = 100
Private difx As Short = -1
Private dify 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 Form_Arrange()

 x = (DrawingArea1.W / 2) - (W / 2)
 y = (DrawingArea1.H / 2) - (H / 2)

End


Public Sub DrawingArea1_MouseDown()

' Verifica se è stato cliccato all'interno del rettangolo (o quadrato):
 If (Mouse.X < x) Or (Mouse.X > (x + W)) Or (Mouse.Y < y) Or (Mouse.Y > (y + H)) Then Return 
 ' oppure:  If (x \ Mouse.X) + (Mouse.X \ (x + W)) + (y \ Mouse.Y) + (Mouse.Y \ (y + H)) > 0 Then Return

 difx = x - Mouse.X
 dify = y - Mouse.Y
 
End


Public Sub DrawingArea1_MouseMove()

 If difx == -1 Then Return
 x = Mouse.X + difx
 y = Mouse.Y + dify
 With DrawingArea1
   .Mouse = 18
   .Refresh
 End With
 
End


Public Sub DrawingArea1_MouseUp()

 difx = -1
 DrawingArea1.Mouse = Mouse.Default

End


Public Sub DrawingArea1_Draw()

 With Paint
   .Rectangle(x, y, W, H, 0.0)
   .stroke
   .end
 End With

End

In quest'altro codice si farà uso delle risorse della Classe Rect per operare idealmente sul rettangolo che sarà disegnato e spostato sulla DrawingArea. [nota 1]

Private DrawingArea1 As DrawingArea
Private rc As Rect
Private x As Short
Private y 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 Form_Arrange()

' Viene creato un Oggetto del tipo "Rect", quale rettangolo ideale i cui parametri saranno passati al Metodo ".Rectangle()" della "DrawingArea" per il disegno effettivo del quadrilatero:
 rc = New Rect((DrawingArea1.W / 2) - 20, (DrawingArea1.H / 2) - 10, 80, 40)

End


Public Sub DrawingArea1_MouseDown()

 With rc
   If Not .Contains(Mouse.X, Mouse.Y) Then Return 
   x = Mouse.X - .X
   y = Mouse.Y - .Y
 End With 

End


Public Sub DrawingArea1_MouseMove()

 With rc
   If Not .Contains(Mouse.X, Mouse.Y) Then Return 
   .X = Mouse.X - x
   .Y = Mouse.Y - y
 End With
 With DrawingArea1
  .Mouse = 18
  .Refresh
 End With

End


Public Sub DrawingArea1_MouseUp()

 DrawingArea1.Mouse = Mouse.Default

End


Public Sub DrawingArea1_Draw()

 With Paint
   .Brush = .Color(Color.Red)
   .Rectangle(rc.X, rc.Y, rc.W, rc.H, 0)
   .Fill
   .End
 End With

End


Note

[1] Vedere anche: