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

Da Gambas-it.org - Wikipedia.
 
(20 versioni intermedie di uno stesso utente non sono mostrate)
Riga 8: Riga 8:
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
   
 
   
 
   With Me
 
   With Me
Riga 17: Riga 17:
 
   
 
   
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
    .X = 0
 
    .Y = 0
 
 
     .Background = Color.Orange
 
     .Background = Color.Orange
 
   End With
 
   End With
 
   
 
   
  '''End'''
+
  End
 
   
 
   
  <FONT color=gray>' ''Gestisce l'evento della pressione del tasto del mouse sulla DrawingArea''</font>
+
  '''Public''' Sub DrawingArea1_MouseDown()   
+
  <FONT color=gray>' ''Gestisce l'Evento della pressione del tasto del mouse sulla DrawingArea''</font>
 
+
  Public Sub DrawingArea1_MouseDown()   
  x = Mouse.X  <FONT color=gray>' ''prende la coordinata x del punto ove si trova il mouse in quel momento''</font>
+
   y = Mouse.Y   <FONT color=gray>' ''prende la coordinata y del punto ove si trova il mouse in quel momento''</font>
+
<FONT color=gray>' ''Prende le coordinata x e y del punto ove si trova il mouse in quel momento:''</font>
 +
  x = Mouse.X
 +
   y = Mouse.Y
 +
<FONT color=gray>' ''Attiva l'Evento "_Draw()" della "DrawingArea":''</font>
 
   DrawingArea1.Refresh
 
   DrawingArea1.Refresh
 
    
 
    
  '''End'''
+
  End
 
   
 
   
  <FONT color=gray>' ''Gestisce l'evento dello spostamento del mouse sulla DrawingArea''</font>
+
  '''Public''' Sub DrawingArea1_MouseMove()   
+
  <FONT color=gray>' ''Gestisce l'Evento dello spostamento del mouse sulla "DrawingArea".''</font>
 +
  Public Sub DrawingArea1_MouseMove()   
 
   
 
   
 
   x = Mouse.X
 
   x = Mouse.X
Riga 41: Riga 43:
 
   DrawingArea1.Refresh
 
   DrawingArea1.Refresh
 
    
 
    
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseUp()
+
  Public Sub DrawingArea1_MouseUp()
 
    
 
    
 
   DrawingArea1.Mouse = Mouse.Default   
 
   DrawingArea1.Mouse = Mouse.Default   
 
    
 
    
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_Draw()
+
  Public Sub DrawingArea1_Draw()
 
   
 
   
 
   With Paint
 
   With Paint
 
     .Font.Size = 20
 
     .Font.Size = 20
    .DrawText("abc", x, y)  <FONT color=gray>' ''è il testo che andremo a spostare nella DrawingArea''</font>
+
<FONT color=gray>' ''Disegna il testo, che sarà poi spostato, nella DrawingArea:''</font>
 +
    .DrawText("abc", x, y)
 
     .End
 
     .End
 
   End With
 
   End With
 
      
 
      
  '''End'''
+
  End
  
  
Riga 67: Riga 72:
 
  End Struct
 
  End Struct
 
  Private sstt As New STRUTTURA[]
 
  Private sstt As New STRUTTURA[]
Private st As STRUTTURA
 
 
  Private DrawingArea1 As DrawingArea
 
  Private DrawingArea1 As DrawingArea
 
  Private i As Integer
 
  Private i As Integer
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
   
 
   
 
   With Me
 
   With Me
Riga 81: Riga 85:
 
   
 
   
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
    .X = 0
 
    .Y = 0
 
 
     .Background = Color.Orange
 
     .Background = Color.Orange
 +
    .Font.Size = 20
 
   End With
 
   End With
 
   
 
   
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseDown()
+
  Public Sub DrawingArea1_MouseDown()
 +
 +
  Dim st As STRUTTURA = New STRUTTURA
 
   
 
   
  st = New STRUTTURA
 
 
   sstt.Push(st)
 
   sstt.Push(st)
 
   sstt[sstt.Max].xx = Mouse.X
 
   sstt[sstt.Max].xx = Mouse.X
Riga 98: Riga 103:
 
   DrawingArea1.Refresh
 
   DrawingArea1.Refresh
 
   
 
   
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseMove()   
+
   
 +
Public Sub DrawingArea1_MouseMove()   
 
   
 
   
 
   sstt[sstt.Max].xx = Mouse.X
 
   sstt[sstt.Max].xx = Mouse.X
Riga 107: Riga 113:
 
   DrawingArea1.Refresh
 
   DrawingArea1.Refresh
 
    
 
    
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseUp()
+
   
 +
Public Sub DrawingArea1_MouseUp()
 
    
 
    
 
   DrawingArea1.Mouse = Mouse.Default   
 
   DrawingArea1.Mouse = Mouse.Default   
 
    
 
    
  '''End'''
+
  End
 +
 +
 +
Public Sub DrawingArea1_Draw()
 +
 +
  With Paint
 +
    For c As Short = 0 To xx.Max
 +
      .DrawText("abc" & CStr(ii[c]), xx[c], yy[c])
 +
    Next
 +
    .End
 +
  End With
 +
 +
End
 +
Il precedente codice può essere ovviamente scritto utilizzando vettori di tipi di dati nativi anziché di tipo ''Struttura''.
 +
Private DrawingArea1 As DrawingArea
 +
Private xx As New Integer[]
 +
Private yy As New Integer[]
 +
Private ii As New String[]
 +
Private i As Integer
 +
 +
 +
Public Sub Form_Open()
 +
 +
  With Me
 +
    .W = Screen.AvailableWidth
 +
    .H = Screen.AvailableHeight
 +
    .Arrangement = Arrange.Fill
 +
  End With
 +
 +
  With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
 +
    .Background = Color.Orange
 +
    .Font.Size = 20
 +
  End With
 +
 +
End
 +
 +
 +
Public Sub DrawingArea1_MouseDown()
 +
 +
  xx.Push(Mouse.X)
 +
  yy.Push(Mouse.Y)
 +
 +
  Inc i
 +
  ii.Push(i)
 +
 +
DrawingArea1.Refresh
 +
 +
End
 +
 +
 +
Public Sub DrawingArea1_MouseMove() 
 
   
 
   
  '''Public''' Sub DrawingArea1_Draw()
+
  xx[xx.Max] = Mouse.X
    
+
  yy[yy.Max] = Mouse.Y
  Dim c As Short
+
 +
  DrawingArea1.Refresh
 +
 +
End
 +
 +
 +
  Public Sub DrawingArea1_Draw()
 +
 +
   With Paint
 +
    For c As Short = 0 To xx.Max
 +
      .DrawText("abc" & CStr(ii[c]), xx[c], yy[c])
 +
    Next
 +
    .End
 +
  End With
 
   
 
   
  For c = 0 To sstt.Max
+
  End
    With Paint
 
      .Font.Size = 20
 
      .DrawText(sstt[c].tx, sstt[c].xx, sstt[c].yy)
 
    End With
 
  Next
 
  Paint.End
 
 
 
  '''End'''
 
  
  
Il '''terzo''' codice, più complesso, consente di spostare il testo, dopo essere stato disegnato sulla "DrawingArea", mantenendo il puntatore del mouse premuto nel punto dell'immagine, ove si è cliccato. <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
+
Il '''terzo''' codice, più complesso, consente di spostare il testo sulla "DrawingArea", conservando la posizione del puntatore del mouse premuto nel punto dell'immagine, ove si è cliccato. <SUP>&#091;[[#Note|nota 1]]&#093;</sup>
 
  Private DrawingArea1 As DrawingArea
 
  Private DrawingArea1 As DrawingArea
 
  Private Const TESTO As String = "Abcde"
 
  Private Const TESTO As String = "Abcde"
Riga 138: Riga 200:
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
   
 
   
 
   With Me
 
   With Me
Riga 147: Riga 209:
 
   
 
   
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
    .X = 0
 
    .Y = 0
 
 
     .Background = Color.Orange
 
     .Background = Color.Orange
 +
    .Font.Size = 20
 
   End With
 
   End With
 
   
 
   
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub Form_Arrange()
+
   
 +
Public Sub Form_Arrange()
 
   
 
   
 
   Dim w, h As Short
 
   Dim w, h As Short
Riga 164: Riga 226:
 
   rc = New Rect((DrawingArea1.W / 2) - (w / 2), (DrawingArea1.H / 2) - (h / 2), w, h)
 
   rc = New Rect((DrawingArea1.W / 2) - (w / 2), (DrawingArea1.H / 2) - (h / 2), w, h)
 
   
 
   
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  '''Public''' Sub DrawingArea1_Draw()
+
  Public Sub DrawingArea1_Draw()
 
    
 
    
 
   With Paint
 
   With Paint
    .Font.Size = 20
 
 
     .DrawText(TESTO, rc.X, rc.Y, rc.W, rc.H, Align.Normal)
 
     .DrawText(TESTO, rc.X, rc.Y, rc.W, rc.H, Align.Normal)
 
     .End
 
     .End
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 +
 
    
 
    
  '''Public''' Sub DrawingArea1_MouseDown()
+
  Public Sub DrawingArea1_MouseDown()
 
    
 
    
 
   With rc
 
   With rc
Riga 185: Riga 247:
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 +
 
    
 
    
  '''Public''' Sub DrawingArea1_MouseMove()
+
  Public Sub DrawingArea1_MouseMove()
 
    
 
    
 
   With rc
 
   With rc
Riga 199: Riga 262:
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseUp()
+
   
 +
Public Sub DrawingArea1_MouseUp()
 
    
 
    
 
   DrawingArea1.Mouse = Mouse.Default
 
   DrawingArea1.Mouse = Mouse.Default
 
    
 
    
  '''End'''
+
  End
  
  
Il seguente '''quarto''' codice non fa uso della Classe "Rect" e anch'esso, tenendo conto del rettangolo immaginario formato dalle dimensioni del testo, consentirà lo spostamento del testo, solo se si cliccherà all'interno di tale rettangolo: <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
+
Il seguente '''quarto''' codice non fa uso della Classe "Rect" e anch'esso, tenendo conto del rettangolo immaginario formato dalle dimensioni del testo, consentirà lo spostamento del testo, solo se si cliccherà all'interno di tale rettangolo: <SUP>&#091;[[#Note|nota 1]]&#093;</sup>
 
  Private DrawingArea1 As DrawingArea
 
  Private DrawingArea1 As DrawingArea
 
  Private x As Short
 
  Private x As Short
Riga 214: Riga 278:
 
  Private w As Short
 
  Private w As Short
 
  Private h As Short
 
  Private h As Short
  Private difx As Short = -1
+
  Private difx As Short
 
  Private dify As Short
 
  Private dify As Short
 
  Private Const TESTO As String = "Abcde"
 
  Private Const TESTO As String = "Abcde"
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
   
 
   
 
   With Me
 
   With Me
Riga 228: Riga 292:
 
   
 
   
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
 
   With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
    .X = 0
 
    .Y = 0
 
 
     .Background = Color.Orange
 
     .Background = Color.Orange
 
   End With
 
   End With
 
   
 
   
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub Form_Arrange()
+
   
 +
Public Sub Form_Arrange()
 
    
 
    
 
   With DrawingArea1.Font
 
   With DrawingArea1.Font
Riga 246: Riga 309:
 
   y = (DrawingArea1.H / 2) - (h / 2)
 
   y = (DrawingArea1.H / 2) - (h / 2)
 
   
 
   
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseDown()
+
  Public Sub DrawingArea1_MouseDown()
 
   
 
   
 +
  difx = -1
 
  <FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo immaginario formato dalle dimensioni del testo:''</font>
 
  <FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo immaginario formato dalle dimensioni del testo:''</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
 
   dify = y - Mouse.Y
 
   dify = y - Mouse.Y
 
   
 
   
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseMove()
+
  Public Sub DrawingArea1_MouseMove()
 
   
 
   
 
   If difx == -1 Then Return
 
   If difx == -1 Then Return
Riga 268: Riga 335:
 
   End With
 
   End With
 
   
 
   
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseUp()
+
  Public Sub DrawingArea1_MouseUp()
 
   
 
   
  difx = -1
 
 
   DrawingArea1.Mouse = Mouse.Default
 
   DrawingArea1.Mouse = Mouse.Default
 
   
 
   
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DrawingArea1_Draw()
+
  Public Sub DrawingArea1_Draw()
 
   
 
   
 
   With Paint
 
   With Paint
Riga 284: Riga 352:
 
   End With
 
   End With
 
   
 
   
  '''End'''
+
  End
 +
Il seguente codice, molto simile al precedente, fa uso del sub-Metodo ".Contains()" fornito da alcune Classi che dispongono della Proprietà ".Font".
 +
Private DrawingArea1 As DrawingArea
 +
Private Const TESTO As String = "Abcde"
 +
Private x As Short
 +
Private y As Short
 +
Private psx As Short
 +
Private psy As Short
 +
Private mx As Short
 +
Private my As Short
 +
 +
 +
Public Sub Form_Open()
 +
 +
  With Me
 +
    .W = Screen.AvailableWidth
 +
    .H = Screen.AvailableHeight
 +
    .Arrangement = Arrange.Fill
 +
  End With
 +
 +
  With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
 +
    .Background = Color.Orange
 +
    .Font.Size = 20
 +
  End With
 +
 +
End
 +
 +
 +
Public Sub Form_Arrange()
 +
 +
  With DrawingArea1
 +
    x = (.W / 2) - (.Font.TextSize(TESTO).W / 2)
 +
    y = (.H / 2) - (.Font.TextSize(TESTO).H / 2)
 +
  End With
 +
 +
  psx = x
 +
  psy = y
 +
 +
End
 +
 +
 +
Public Sub DrawingArea1_Draw()
 +
 +
  With Paint
 +
    .DrawText(TESTO, x, y, .Font.TextSize(TESTO).W, .Font.TextSize(TESTO).H, Align.Normal)
 +
    .End
 +
  End With
 +
 
 +
End
 +
 +
 +
Public Sub DrawingArea1_MouseDown()
 +
 +
  mx = -1
 +
<FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo immaginario compreso dal testo impostato:''</font>
 +
  If Not DrawingArea1.Font.TextSize(TESTO).Contains(Mouse.X - psx, Mouse.Y - psy) Then Return
 +
  mx = Mouse.X
 +
  my = Mouse.Y
 +
 +
End
 +
 +
 +
Public Sub DrawingArea1_MouseMove()
 +
 +
  With DrawingArea1
 +
<FONT Color=gray>' ''Verifica se è stato cliccato all'interno del rettangolo immaginario compreso dal testo impostato:''</font>
 +
    If mx == -1 Then Return
 +
    x = psx - (mx - Mouse.X)
 +
    y = psy - (my - Mouse.Y)
 +
    .Mouse = 18
 +
    .Refresh
 +
  End With
 +
 +
End
 +
 +
 +
Public Sub DrawingArea1_MouseUp()
 +
 +
  DrawingArea1.Mouse = Mouse.Default
 +
  psx = x
 +
  psy = y
 +
 +
End
  
  
Il seguente esempio ottiene il medesimo effetto del codice precedente: <SUP>&#091;[[#Note|Nota 2]]&#093;</sup>
+
Il seguente '''quinto''' codice ottiene il medesimo effetto dei codici precedenti: <SUP>&#091;[[#Note|Nota 2]]&#093;</sup>
 
  Private bOk As Boolean      <FONT color=gray>' ''Booleano attivo se puntatore è in area scritta''</font>
 
  Private bOk As Boolean      <FONT color=gray>' ''Booleano attivo se puntatore è in area scritta''</font>
 
  Private bDis As Boolean    <FONT color=gray>' ''Booleano attivo se dati provengono da Form_Open''</font>
 
  Private bDis As Boolean    <FONT color=gray>' ''Booleano attivo se dati provengono da Form_Open''</font>
Riga 301: Riga 451:
 
   
 
   
 
      
 
      
  '''Public''' Sub Form_Open()   
+
  Public Sub Form_Open()   
 
   
 
   
 
  <FONT color=gray>' ''Inserisce il testo al centro dell'area di disegno dando le giuste indicazioni a DrawingArea1_Draw:''</font>
 
  <FONT color=gray>' ''Inserisce il testo al centro dell'area di disegno dando le giuste indicazioni a DrawingArea1_Draw:''</font>
Riga 308: Riga 458:
 
   bOk = True   
 
   bOk = True   
 
        
 
        
  '''End'''
+
  End
     
+
  '''Public''' Sub DrawingArea1_MouseDown()   
+
 
 +
  Public Sub DrawingArea1_MouseDown()   
 
        
 
        
 
  <FONT color=gray>' ''Quando si preme il tasto sinistro passa la posizione del mouse alle variabili x e y:''</font>
 
  <FONT color=gray>' ''Quando si preme il tasto sinistro passa la posizione del mouse alle variabili x e y:''</font>
Riga 327: Riga 478:
 
     Endif   
 
     Endif   
 
   Endif   
 
   Endif   
     
 
'''End'''
 
 
   
 
   
  '''Public''' Sub DrawingArea1_MouseMove()   
+
  End
     
+
 +
Public Sub DrawingArea1_MouseMove()   
 +
 
  <FONT color=gray>' ''Se il booleano OK è vero passa alle variabili la posizione e attiva il disegno:''</font>
 
  <FONT color=gray>' ''Se il booleano OK è vero passa alle variabili la posizione e attiva il disegno:''</font>
 
   If bOk Then   
 
   If bOk Then   
Riga 338: Riga 489:
 
     DrawingArea1.Refresh   
 
     DrawingArea1.Refresh   
 
   Endif   
 
   Endif   
     
+
     
+
End
  '''End'''
+
   
     
+
 
  '''Public''' Sub DrawingArea1_Draw()   
+
  Public Sub DrawingArea1_Draw()   
     
+
 
  <FONT color=gray>' ''Se il booleano "bOK" è vero, passa a controllare il booleano eventualmente attivato da "Form_Open".''   
 
  <FONT color=gray>' ''Se il booleano "bOK" è vero, passa a controllare il booleano eventualmente attivato da "Form_Open".''   
  ' ''Se quest 'ultimo è vero crea il testo scritto al centro dell'area di disegno,''
+
  ' ''Se quest'ultimo è vero crea il testo scritto al centro dell'area di disegno, altrimenti disegna il testo sulle indicazioni del mouse:''</font>   
' ''altrimenti disegna il testo sulle indicazioni del mouse:''</font>   
 
 
   If bOk Then           
 
   If bOk Then           
 
     With Paint   
 
     With Paint   
Riga 364: Riga 514:
 
   Endif   
 
   Endif   
 
        
 
        
  '''End'''
+
  End
 +
 +
 +
Public Sub DrawingArea1_MouseUp() 
 
   
 
   
'''Public''' Sub DrawingArea1_MouseUp() 
+
  <FONT color=gray>' ''Quando si rilascia il bottone del mouse cambia l'aspetto del mouse a quello di default e memorizza nelle variabili la posizione iniziale della scritta per essere pronti al nuovo trascinamento:''</font>
     
 
  <FONT color=gray>' ''Quando si rilascia il bottone del mouse cambia l'aspetto del mouse a quello di default''
 
' ''e memorizza nelle variabili la posizione iniziale della scritta per essere pronti al nuovo trascinamento:''</font>
 
 
   DrawingArea1.Mouse = Mouse.Default   
 
   DrawingArea1.Mouse = Mouse.Default   
 
   xIn = x + xDif   
 
   xIn = x + xDif   
 
   yIn = y + yDif   
 
   yIn = y + yDif   
 
        
 
        
  '''End'''
+
  End
     
+
   
'''Public''' Sub ridimensiona(size As Float) As Float  
 
 
        
 
        
 +
Public Sub ridimensiona(size As Float) As Float 
 +
 
  <FONT color=gray>' ''Proporziona il testo nell'area di disegno:''</font>
 
  <FONT color=gray>' ''Proporziona il testo nell'area di disegno:''</font>
 
   If Paint.Device = DrawingArea1 Then   
 
   If Paint.Device = DrawingArea1 Then   
Riga 385: Riga 536:
 
   Endif     
 
   Endif     
 
        
 
        
  '''End'''
+
  End
  
  
Riga 391: Riga 542:
 
=Note=
 
=Note=
 
[1] Vedere anche:
 
[1] Vedere anche:
<BR> - [[Spostare_con_il_mouse_un_rettangolo_in_una_DrawingArea|Spostare con il mouse un rettangolo in una DrawingArea]]
+
* [[Spostare con il mouse un rettangolo in una DrawingArea]]
<BR> - [[Spostare_con_il_mouse_un'immagine_in_una_DrawingArea|Spostare con il mouse un'immagine in una DrawingArea]]
+
* [[Spostare con il mouse un'immagine caricata in una DrawingArea]]
 +
* [[Creare, spostare e distruggere un'immagine in una DrawingArea]]
  
 
[2] Il codice è stato realizzato dall'utente [http://www.gambas-it.org/smf/index.php?action=profile;u=1249''Gianluigi''] del forum ''www.gambas-it.org''.
 
[2] Il codice è stato realizzato dall'utente [http://www.gambas-it.org/smf/index.php?action=profile;u=1249''Gianluigi''] del forum ''www.gambas-it.org''.

Versione attuale delle 14:50, 27 giu 2024

Mostreremo alcuni codici per spostare con il mouse un testo all'interno di una DrawingArea.


Il primo codice più semplice e basilare per effettuare uno spostamento del testo nella DrawingArea dopo averlo disegnato: cliccare con il mouse sulla "DrawingArea", per disegnare il testo e, mantenendo cliccato, spostare il mouse sulla superficie.

Private DrawingArea1 As DrawingArea
Private x As Integer
Private y As Integer


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With

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

End


' Gestisce l'Evento della pressione del tasto del mouse sulla DrawingArea
Public Sub DrawingArea1_MouseDown()   

' Prende le coordinata x e y del punto ove si trova il mouse in quel momento:
 x = Mouse.X
 y = Mouse.Y
' Attiva l'Evento "_Draw()" della "DrawingArea":
 DrawingArea1.Refresh
 
End


' Gestisce l'Evento dello spostamento del mouse sulla "DrawingArea".
Public Sub DrawingArea1_MouseMove()   

 x = Mouse.X
 y = Mouse.Y
 DrawingArea1.Mouse = 18
 DrawingArea1.Refresh
 
End


Public Sub DrawingArea1_MouseUp()
 
 DrawingArea1.Mouse = Mouse.Default  
 
End


Public Sub DrawingArea1_Draw()

 With Paint
   .Font.Size = 20
' Disegna il testo, che sarà poi spostato, nella DrawingArea:
   .DrawText("abc", x, y)
   .End
 End With
   
End


Il secondo codice consente di aggiungere più stringhe testuali alla DrawingArea. Tali stringhe, mantenendo cliccato con il mouse subito dopo averle create, possono essere spostate sulla superficie della "DrawingArea":

Public Struct STRUTTURA
  xx As Integer
  yy As Integer
  tx As String
End Struct
Private sstt As New STRUTTURA[]
Private DrawingArea1 As DrawingArea
Private i As Integer


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.Orange
   .Font.Size = 20
 End With

End


Public Sub DrawingArea1_MouseDown()

 Dim st As STRUTTURA = New STRUTTURA

 sstt.Push(st)
 sstt[sstt.Max].xx = Mouse.X
 sstt[sstt.Max].yy = Mouse.Y
 Inc i
 sstt[sstt.Max].tx = "abc " & CStr(i)
 DrawingArea1.Refresh

End


Public Sub DrawingArea1_MouseMove()   

 sstt[sstt.Max].xx = Mouse.X
 sstt[sstt.Max].yy = Mouse.Y
 DrawingArea1.Mouse = 18
 DrawingArea1.Refresh
 
End


Public Sub DrawingArea1_MouseUp()
 
 DrawingArea1.Mouse = Mouse.Default  
 
End


Public Sub DrawingArea1_Draw()

 With Paint
   For c As Short = 0 To xx.Max
     .DrawText("abc" & CStr(ii[c]), xx[c], yy[c])
   Next
   .End
 End With

End

Il precedente codice può essere ovviamente scritto utilizzando vettori di tipi di dati nativi anziché di tipo Struttura.

Private DrawingArea1 As DrawingArea
Private xx As New Integer[]
Private yy As New Integer[]
Private ii As New String[]
Private i As Integer


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.Orange
   .Font.Size = 20
 End With

End


Public Sub DrawingArea1_MouseDown()

 xx.Push(Mouse.X)
 yy.Push(Mouse.Y)

 Inc i
 ii.Push(i)

DrawingArea1.Refresh

End


Public Sub DrawingArea1_MouseMove()   

 xx[xx.Max] = Mouse.X
 yy[yy.Max] = Mouse.Y

 DrawingArea1.Refresh

End


Public Sub DrawingArea1_Draw()

 With Paint
   For c As Short = 0 To xx.Max
     .DrawText("abc" & CStr(ii[c]), xx[c], yy[c])
   Next
   .End
 End With

End


Il terzo codice, più complesso, consente di spostare il testo sulla "DrawingArea", conservando la posizione del puntatore del mouse premuto nel punto dell'immagine, ove si è cliccato. [nota 1]

Private DrawingArea1 As DrawingArea
Private Const TESTO As String = "Abcde"
Private rc As Rect
Private x As Short
Private y As Short


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.Orange
   .Font.Size = 20
 End With

End


Public Sub Form_Arrange()

 Dim w, h As Short

 w = DrawingArea1.Font.TextSize(TESTO).W
 h = DrawingArea1.Font.TextSize(TESTO).H

' Imposta un quadrilatero rettangolo immaginario, adattato alle dimensioni del testo, che sarà usato per consentire di spostare il testo solo se si è cliccato al suo interno:
 rc = New Rect((DrawingArea1.W / 2) - (w / 2), (DrawingArea1.H / 2) - (h / 2), w, h)

End


Public Sub DrawingArea1_Draw()
 
 With Paint
   .DrawText(TESTO, rc.X, rc.Y, rc.W, rc.H, Align.Normal)
   .End
 End With
  
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


Il seguente quarto codice non fa uso della Classe "Rect" e anch'esso, tenendo conto del rettangolo immaginario formato dalle dimensioni del testo, consentirà lo spostamento del testo, solo se si cliccherà all'interno di tale rettangolo: [nota 1]

Private DrawingArea1 As DrawingArea
Private x As Short
Private y As Short
Private w As Short
Private h As Short
Private difx As Short
Private dify As Short
Private Const TESTO As String = "Abcde"


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With

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

End


Public Sub Form_Arrange()
 
 With DrawingArea1.Font
   .Size = 20
   w = .TextSize(TESTO).W
   h = .TextSize(TESTO).H
 End With

 x = (DrawingArea1.W / 2) - (w / 2)
 y = (DrawingArea1.H / 2) - (h / 2)

End


Public Sub DrawingArea1_MouseDown()

 difx = -1
' Verifica se è stato cliccato all'interno del rettangolo immaginario formato dalle dimensioni del testo:
 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()

 DrawingArea1.Mouse = Mouse.Default

End


Public Sub DrawingArea1_Draw()

 With Paint
   .DrawText(TESTO, x, y, w, h, Align.Normal)
   .End
 End With

End

Il seguente codice, molto simile al precedente, fa uso del sub-Metodo ".Contains()" fornito da alcune Classi che dispongono della Proprietà ".Font".

Private DrawingArea1 As DrawingArea
Private Const TESTO As String = "Abcde"
Private x As Short
Private y As Short
Private psx As Short
Private psy As Short
Private mx As Short
Private my As Short


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.Orange
   .Font.Size = 20
 End With

End


Public Sub Form_Arrange()

 With DrawingArea1
   x = (.W / 2) - (.Font.TextSize(TESTO).W / 2)
   y = (.H / 2) - (.Font.TextSize(TESTO).H / 2)
 End With

 psx = x
 psy = y

End


Public Sub DrawingArea1_Draw()

 With Paint
   .DrawText(TESTO, x, y, .Font.TextSize(TESTO).W, .Font.TextSize(TESTO).H, Align.Normal)
   .End
 End With
 
End


Public Sub DrawingArea1_MouseDown()

 mx = -1
' Verifica se è stato cliccato all'interno del rettangolo immaginario compreso dal testo impostato:
 If Not DrawingArea1.Font.TextSize(TESTO).Contains(Mouse.X - psx, Mouse.Y - psy) Then Return 
 mx = Mouse.X
 my = Mouse.Y

End


Public Sub DrawingArea1_MouseMove()

 With DrawingArea1
' Verifica se è stato cliccato all'interno del rettangolo immaginario compreso dal testo impostato:
   If mx == -1 Then Return 
   x = psx - (mx - Mouse.X)
   y = psy - (my - Mouse.Y)
   .Mouse = 18
   .Refresh
 End With

End


Public Sub DrawingArea1_MouseUp()

 DrawingArea1.Mouse = Mouse.Default
 psx = x
 psy = y

End


Il seguente quinto codice ottiene il medesimo effetto dei codici precedenti: [Nota 2]

Private bOk As Boolean      ' Booleano attivo se puntatore è in area scritta
Private bDis As Boolean     ' Booleano attivo se dati provengono da Form_Open
Private xIn As Integer      ' Memorizza la posizione del testo al momento che agiamo sul tasto sinistro del mouse
Private yIn As Integer      ' Idem
Private xDif As Integer     ' Memorizza la distanza tra la posizione del puntatore e quella del testo
Private yDif As Integer     ' Idem
Private x As Integer        ' Memorizza la posizione del puntatore
Private y As Integer        ' Idem
Private iAlt As Integer     ' Memorizza l'altezza del testo
Private iLarg As Integer    ' Memorizza la larghezza del testo
Private sTesto As String    ' Memorizza la scritta del testo

    
Public Sub Form_Open()  

' Inserisce il testo al centro dell'area di disegno dando le giuste indicazioni a DrawingArea1_Draw:
 sTesto = "Testo di prova"  
 bDis = True  
 bOk = True  
      
End

  
Public Sub DrawingArea1_MouseDown()  
      
' Quando si preme il tasto sinistro passa la posizione del mouse alle variabili x e y:
 x = Mouse.X    
 y = Mouse.Y   
' Ottiene la differenza fra le posizioni x e y del testo rispetto a quelle del mouse:
 xDif = xIn - x    
 yDif = yIn - y  
' Si accerta che il booleano OK sia su false:
 bOk = False  
' Se il mouse si trova nell'area della scritta cambia aspetto e porta il booleano OK a vero:
 If x >= xIn And x <= (xIn + iLarg) Then  
   If y <= yIn And y >= (yIn - iAlt) Then         
     DrawingArea1.Mouse = Mouse.SizeAll  
     bOk = True          
   Endif  
 Endif  

End

Public Sub DrawingArea1_MouseMove()  

' Se il booleano OK è vero passa alle variabili la posizione e attiva il disegno:
 If bOk Then  
   x = Mouse.X    
   y = Mouse.Y    
   DrawingArea1.Refresh  
 Endif  

End

  
Public Sub DrawingArea1_Draw()  

' Se il booleano "bOK" è vero, passa a controllare il booleano eventualmente attivato da "Form_Open".   
' Se quest'ultimo è vero crea il testo scritto al centro dell'area di disegno, altrimenti disegna il testo sulle indicazioni del mouse:  
 If bOk Then           
   With Paint  
     .Font.Size = ridimensiona(22)  
     If bDis Then  
       iLarg = .Font.RichTextWidth(sTesto)  
       x = (DrawingArea1.W / 2) - (iLarg / 2)  
       xIn = x  
       iAlt = .Font.RichTextHeight(sTesto)  
       y = (DrawingArea1.H / 2) + (iAlt / 2)  
       yIn = y  
       bDis = False     
     Endif        
     .DrawText(sTesto, x + xDif, y + yDif)        
     .End  
   End With       
 Endif  
      
End


Public Sub DrawingArea1_MouseUp()  

' Quando si rilascia il bottone del mouse cambia l'aspetto del mouse a quello di default e memorizza nelle variabili la posizione iniziale della scritta per essere pronti al nuovo trascinamento:
 DrawingArea1.Mouse = Mouse.Default  
 xIn = x + xDif  
 yIn = y + yDif  
      
End

     
Public Sub ridimensiona(size As Float) As Float  

' Proporziona il testo nell'area di disegno:
 If Paint.Device = DrawingArea1 Then  
   Return (size * 0.744)  
 Else  
   Return size  
 Endif    
     
End


Note

[1] Vedere anche:

[2] Il codice è stato realizzato dall'utente Gianluigi del forum www.gambas-it.org.