Differenze tra le versioni di "Spostare con il mouse un rettangolo in una DrawingArea"
Da Gambas-it.org - Wikipedia.
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. | Nel seguente esempio si potrà spostare con il mouse sulla superficie di una ''DrawingArea'' un rettangolo (o un quadrato) precedentemente disegnato. | ||
<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 x1 As Short = 100 |
+ | Private y1 As Short = 100 | ||
+ | Private Const X2 As Short = 200 | ||
+ | Private Const Y2 As Short = 100 | ||
+ | Private difx As Short = -1 | ||
+ | Private dify As Short | ||
+ | |||
+ | |||
+ | '''Public''' Sub DrawingArea1_MouseDown() | ||
+ | |||
+ | <FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo (o quadrato):''</font> | ||
+ | If (x1 \ Mouse.X) + (Mouse.X \ (x1 + X2)) + (y1 \ Mouse.Y) + (Mouse.Y \ (y1 + Y2)) > 0 Then Return | ||
+ | |||
+ | difx = x1 - Mouse.X | ||
+ | dify = y1 - Mouse.Y | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | '''Public''' Sub DrawingArea1_MouseMove() | ||
+ | |||
+ | If difx == -1 Then Return | ||
+ | x1 = Mouse.X + difx | ||
+ | y1 = 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(x1, y1, 200, 100, 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''. <SUP>[[[#Note|Nota 1]]]</sup> | ||
Private rc As Rect | Private rc As Rect | ||
Private x As Short | Private x As Short | ||
Riga 57: | Riga 104: | ||
=Note= | =Note= | ||
− | [1] Vedere anche: [[Spostare_con_il_mouse_un'immagine_in_una_DrawingArea|Spostare con il mouse un'immagine in una DrawingArea]] | + | [1] Vedere anche: |
+ | <BR> - [[Spostare_con_il_mouse_un'immagine_in_una_DrawingArea|Spostare con il mouse un'immagine in una DrawingArea]] | ||
+ | <BR> - [[Spostare_con_il_mouse_un_cerchio_in_una_DrawingArea|Spostare con il mouse un cerchio in una DrawingArea]] |
Versione delle 01:31, 28 nov 2021
Nel seguente esempio si potrà spostare con il mouse sulla superficie di una DrawingArea un rettangolo (o un quadrato) precedentemente disegnato.
In particolare durante lo spostamento del rettangolo il puntatore del mouse resterà laddove si è cliccato all'interno del rettangolo.
Private x1 As Short = 100 Private y1 As Short = 100 Private Const X2 As Short = 200 Private Const Y2 As Short = 100 Private difx As Short = -1 Private dify As Short Public Sub DrawingArea1_MouseDown() ' Verifica se è stato cliccato all'interno del rettangolo (o quadrato): If (x1 \ Mouse.X) + (Mouse.X \ (x1 + X2)) + (y1 \ Mouse.Y) + (Mouse.Y \ (y1 + Y2)) > 0 Then Return difx = x1 - Mouse.X dify = y1 - Mouse.Y End Public Sub DrawingArea1_MouseMove() If difx == -1 Then Return x1 = Mouse.X + difx y1 = 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(x1, y1, 200, 100, 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 rc As Rect Private x As Short Private y As Short Public Sub _new() ' 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, 40, 20) 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:
- Spostare con il mouse un'immagine in una DrawingArea
- Spostare con il mouse un cerchio in una DrawingArea