Differenze tra le versioni di "Spostare con il mouse un rettangolo in una DrawingArea"
Da Gambas-it.org - Wikipedia.
(6 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
− | Nel seguente esempio si potrà spostare con il mouse sulla superficie di una ''DrawingArea'' un rettangolo (o un quadrato) precedentemente disegnato. <SUP>[[[#Note| | + | Nel seguente esempio si potrà spostare con il mouse sulla superficie di una ''DrawingArea'' un rettangolo (o un quadrato) precedentemente disegnato. <SUP>[[[#Note|nota 1]]]</sup> |
<BR>In particolare durante lo spostamento del rettangolo il puntatore del mouse resterà laddove si è cliccato all'interno del rettangolo. | <BR>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 x As Short | ||
Private y As Short | Private y As Short | ||
Riga 9: | Riga 10: | ||
'''Public''' Sub _new() | '''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) | x = (DrawingArea1.W / 2) - (W / 2) | ||
Riga 18: | Riga 32: | ||
<FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo (o quadrato):''</font> | <FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo (o quadrato):''</font> | ||
− | If (x \ Mouse.X) + (Mouse.X \ (x + W)) + (y \ Mouse.Y) + (Mouse.Y \ (y + H)) > 0 Then Return | + | If (Mouse.X < x) Or (Mouse.X > (x + W)) Or (Mouse.Y < y) Or (Mouse.Y > (y + H)) Then Return |
+ | <FONT Color=gray>' ''oppure: '''''If (x \ Mouse.X) + (Mouse.X \ (x + W)) + (y \ Mouse.Y) + (Mouse.Y \ (y + H)) > 0 Then Return'''</font> | ||
difx = x - Mouse.X | difx = x - Mouse.X | ||
Riga 54: | Riga 69: | ||
'''End''' | '''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''. <SUP>[[[#Note| | + | In quest'altro codice si farà uso delle risorse della Classe "Rect" per operare idealmente sul rettangolo che sarà disegnato e spostato sulla ''DrawingArea''. <SUP>[[[#Note|nota 1]]]</sup> |
+ | Private DrawingArea1 As DrawingArea | ||
Private rc As Rect | Private rc As Rect | ||
Private x As Short | Private x As Short | ||
Riga 60: | Riga 76: | ||
'''Public''' Sub _new() | '''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() | ||
<FONT Color=gray>' ''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:''</font> | <FONT Color=gray>' ''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:''</font> | ||
− | rc = New Rect((DrawingArea1.W / 2) - 20, (DrawingArea1.H / 2) - 10, 40 | + | rc = New Rect((DrawingArea1.W / 2) - 20, (DrawingArea1.H / 2) - 10, 80, 40) |
'''End''' | '''End''' | ||
Riga 111: | Riga 140: | ||
=Note= | =Note= | ||
[1] Vedere anche: | [1] Vedere anche: | ||
− | + | * [[Spostare con il mouse un cerchio in una DrawingArea]] | |
− | + | * [[Spostare con il mouse un'ellisse in una DrawingArea]] | |
+ | * [[Spostare con il mouse un'immagine caricata in una DrawingArea]] | ||
+ | * [[Creare, spostare e distruggere un'immagine in una DrawingArea]] |
Versione attuale delle 19:12, 19 giu 2023
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: