Differenze tra le versioni di "Creare, spostare e distruggere un'immagine in una DrawingArea"
Da Gambas-it.org - Wikipedia.
(Creata pagina con "Mostriamo un possibile codice per creare più immagini, spostare e distruggere delle immagini sulla ''DrawingArea''. <BR>Gli Oggetti di tipo ''Image'' creati sono assegnati a...") |
|||
(6 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
− | Mostriamo un possibile codice per creare più immagini, | + | 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 im As Image | ||
Private iimm As New Image[] | Private iimm As New Image[] | ||
Riga 10: | Riga 14: | ||
− | Public Sub | + | 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 With | ||
Riga 27: | Riga 30: | ||
Public Sub DrawingArea1_MouseDown() | Public Sub DrawingArea1_MouseDown() | ||
− | If Mouse. | + | If Mouse.Middle Then |
If iimm.Count > 0 Then | If iimm.Count > 0 Then | ||
iimm.Remove(iimm.Max) | iimm.Remove(iimm.Max) | ||
Riga 35: | Riga 38: | ||
Return | Return | ||
Endif | 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) | xx.Push(Mouse.X) | ||
yy.Push(Mouse.Y - 24) | 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) | iimm.Push(im) | ||
Endif | Endif | ||
Riga 47: | Riga 84: | ||
Public Sub DrawingArea1_MouseMove() | Public Sub DrawingArea1_MouseMove() | ||
− | If Mouse. | + | If Mouse.Middle Then Return |
− | xx[xx.Max] = Mouse.X | + | If Mouse.Left Then |
− | yy[yy.Max] = Mouse.Y - | + | 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 | DrawingArea1.Refresh | ||
Riga 57: | Riga 99: | ||
Public Sub DrawingArea1_Draw() | Public Sub DrawingArea1_Draw() | ||
− | + | ||
Dim i As Integer | Dim i As Integer | ||
With Paint | 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 | For i = 0 To iimm.Max | ||
− | + | .DrawImage(iimm[i], xx[i], yy[i], im.W, im.H, Align.Center) | |
Next | Next | ||
.End | .End |
Versione delle 02:28, 20 giu 2023
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