Differenze tra le versioni di "Creare, spostare e distruggere un'immagine in una DrawingArea"

Da Gambas-it.org - Wikipedia.
Riga 45: Riga 45:
 
   
 
   
 
  Public Sub DrawingArea1_MouseDown()
 
  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  
 
   If Mouse.Left Then  
    im = New Image(16, 26, Color.Transparent, Image.Standard)
 
 
     With Paint
 
     With Paint
 
       .Begin(im)
 
       .Begin(im)
Riga 61: Riga 72:
 
   
 
   
 
   If Mouse.Right Then  
 
   If Mouse.Right Then  
    im = New Image(16, 26, Color.Transparent, Image.Standard)
 
 
     With Paint
 
     With Paint
 
       .Begin(im)
 
       .Begin(im)
Riga 83: Riga 93:
 
     iimm.Push(im)
 
     iimm.Push(im)
 
   Endif
 
   Endif
 
 
  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 
 
 
   
 
   
 
   DrawingArea1.Refresh
 
   DrawingArea1.Refresh

Versione delle 18:11, 19 giu 2023

Mostriamo un possibile codice per creare più immagini, spostare e distruggere delle immagini sulla DrawingArea.
Gli Oggetti di tipo Image creati sono assegnati a un vettore di tipo "Image[]".

Con l'esempio, che mostriamo di seguito:

  • 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, e possibile spostarla sempre con il mouse 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
   .padding = 5
   .Arrangement = Arrange.Fill
 End With

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.SoftYellow
 End With

End

Public Sub Form_Open()

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

 With Paint
   .Begin(im)
   .Font.Size = 24
   .Brush = .Color(Color.Blue)
   .DrawText(String.Chr(&1d15f), 0, 14, 0, 0, Align.Center)
   .DrawText(String.Chr(&1d16e), 8, 14, 0, 0, Align.Center)
   .End
 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