Creare, spostare e distruggere un'immagine in una DrawingArea

Da Gambas-it.org - Wikipedia.

Mostriamo un possibile codice per creare più immagini, spostarle e distruggerle sulla DrawingArea.

Nell'esempio, che mostriamo di seguito:

  • gli Oggetti di tipo Image sono creati cliccando sul tasto destro o sinistro del mouse e sono assegnati a un vettore di tipo "Image[]";
  • cliccando con il tasto sinistro si creerà la figura musicale della Croma con gambo e cediglia in alto;
  • cliccando con il tasto destro del mouse, si creerà la figura musicale della Croma con gambo e cediglia in basso;
  • cliccando invece con il tasto centrale del mouse si distruggerà l'Oggetto Image contenuto nell'ultimo elemento corrente del vettore di tipo "Image[]".

Subito dopo la creazione di una singola immagine, mantenendo premuto il tasto di sinistra del mouse, è possibile spostarla manualmente sulla superficie della DrawingArea.

Private DrawingArea1 As DrawingArea
Private im As Image
Private iimm As New Image[]
Private xx As New Short[]
Private yy As New 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.SoftYellow
 End With

End

Public Sub DrawingArea1_MouseDown()

 If Mouse.Middle Then 
   If iimm.Count > 0 Then
     iimm.Remove(iimm.Max)
     xx.Remove(xx.Max)
     yy.Remove(yy.Max)
   Else 
     Return 
   Endif
 Endif  

 im = New Image(16, 26, Color.Transparent, Image.Standard)

 If Mouse.Left Then 
   With Paint
     .Begin(im)
     .Font.Size = 24
     .Brush = .Color(Color.Blue)
     .DrawText(String.Chr(&1d160), 0, 14, 0, 0, Align.Center)
     .End
   End With
   xx.Push(Mouse.X)
   yy.Push(Mouse.Y - 24)
   iimm.Push(im)
 Endif

 If Mouse.Right Then 
   With Paint
     .Begin(im)
     .Font.Size = 24
     .Brush = .Color(Color.Blue)
     .DrawText(String.Chr(&1d15f), 0, 14, 0, 0, Align.Center)
     .End
   End With
   im = im.Mirror(True, False)
   With Paint
     .Begin(im)
     .Font.Size = 24
     .Brush = .Color(Color.Blue)
     .DrawText(String.Chr(&1d16e), 8, 14, 0, 0, Align.Center)
     .End
   End With
   im = im.Mirror(False, True)
 
   xx.Push(Mouse.X - 8)
   yy.Push(Mouse.Y - 20)
   iimm.Push(im)
 Endif

 DrawingArea1.Refresh

End

Public Sub DrawingArea1_MouseMove()

 If Mouse.Middle Then Return 

 If Mouse.Left Then 
   xx[xx.Max] = Mouse.X
   yy[yy.Max] = Mouse.Y - 24
 Else 
    xx[xx.Max] = Mouse.X - 8
    yy[yy.Max] = Mouse.Y - 20
 Endif 

 DrawingArea1.Refresh

End

Public Sub DrawingArea1_Draw()

 Dim i As Integer

 With Paint
   For i = 0 To 50
   .Font.Size = 24
   .DrawText(String.Chr(&1d11a), 10 * i + .Font.TextWidth(String.Chr(&1d11a)), 100, DrawingArea1.W * 0.8, 64, Align.Left)
   Next
   .DrawText(String.Chr(&1d11e), 24, 131, 0, 0, Align.Left)
   .DrawText(String.Chr(&1d134), 48, 132, 0, 0, Align.Left)
   For i = 0 To iimm.Max
   .DrawImage(iimm[i], xx[i], yy[i], im.W, im.H, Align.Center)
   Next 
   .End
 End With

End