Differenze tra le versioni di "Spostare con il mouse un rettangolo in una DrawingArea"

Da Gambas-it.org - Wikipedia.
 
(7 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>&#091;[[#Note|Nota 1]]&#093;</sup>
+
Nel seguente esempio si potrà spostare con il mouse sulla superficie di una ''DrawingArea'' un rettangolo (o un quadrato) precedentemente disegnato. <SUP>&#091;[[#Note|nota 1]]&#093;</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 x1 As Short
+
  Private DrawingArea1 As DrawingArea
  Private y1 As Short
+
Private x As Short
  Private Const X2 As Short = 200
+
  Private y As Short
  Private Const Y2 As Short = 100
+
  Private Const W As Short = 200
 +
  Private Const H As Short = 100
 
  Private difx As Short = -1
 
  Private difx As Short = -1
 
  Private dify As Short
 
  Private dify As Short
Riga 10: Riga 11:
 
  '''Public''' Sub _new()
 
  '''Public''' Sub _new()
 
   
 
   
   x1 = (DrawingArea1.W / 2) - (X2 / 2)
+
   With Me
   y1 = (DrawingArea1.H / 2) - (Y2 / 2)
+
    .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'''
 
  '''End'''
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 (x1 \ Mouse.X) + (Mouse.X \ (x1 + X2)) + (y1 \ Mouse.Y) + (Mouse.Y \ (y1 + Y2)) > 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 = x1 - Mouse.X
+
   difx = x - Mouse.X
   dify = y1 - Mouse.Y
+
   dify = y - Mouse.Y
 
    
 
    
 
  '''End'''
 
  '''End'''
Riga 28: Riga 43:
 
   
 
   
 
   If difx == -1 Then Return
 
   If difx == -1 Then Return
   x1 = Mouse.X + difx
+
   x = Mouse.X + difx
   y1 = Mouse.Y + dify
+
   y = Mouse.Y + dify
 
   With DrawingArea1
 
   With DrawingArea1
 
     .Mouse = 18
 
     .Mouse = 18
Riga 47: Riga 62:
 
   
 
   
 
   With Paint
 
   With Paint
     .Rectangle(x1, y1, 200, 100, 0.0)
+
     .Rectangle(x, y, W, H, 0.0)
 
     .stroke
 
     .stroke
 
     .end
 
     .end
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>&#091;[[#Note|Nota 1]]&#093;</sup>
+
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>&#091;[[#Note|nota 1]]&#093;</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, 20)
+
   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:
<BR> - [[Spostare_con_il_mouse_un'immagine_in_una_DrawingArea|Spostare con il mouse un'immagine in una DrawingArea]]
+
* [[Spostare con il mouse un cerchio in una DrawingArea]]
<BR> - [[Spostare_con_il_mouse_un_cerchio_in_una_DrawingArea|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: