Differenze tra le versioni di "Curvare il testo in una DrawingArea"

Da Gambas-it.org - Wikipedia.
Riga 70: Riga 70:
 
  '''End'''
 
  '''End'''
  
Altro esempio:
+
Altro esempio, nel quale si consentirà di disegnare anche i caratteri speciali come le lettere accentate:
 
  '''Public''' Sub Form_Open()
 
  '''Public''' Sub Form_Open()
 
    
 
    
Riga 92: Riga 92:
 
    
 
    
 
   Dim b As Byte
 
   Dim b As Byte
   Dim bb As Byte[]
+
   Dim s As String
    
+
   Dim testo As String = "Lì e là può di più."
  bb = Byte[].FromString("Linguaggio Gambas")
 
 
    
 
    
 
   With Paint
 
   With Paint
Riga 103: Riga 102:
 
     .Font.Bold = True
 
     .Font.Bold = True
 
     .Brush = .Color(Color.Red)
 
     .Brush = .Color(Color.Red)
     .DrawText(Chr(bb[0]), 0, 0, .Font.TextWidth(Chr(bb[0])), .Font.TextHeight(Chr(bb[0])), Align.Center)
+
    s = String.Mid(testo, 1, 1)
 +
     .DrawText(s, 0, 0, .Font.TextWidth(s), .Font.TextHeight(s), Align.Center)
 
    
 
    
 
  <FONT Color=gray>' ''Imposta la posizione dei restanti caratteri del testo da disegnare ruotato:''</font>
 
  <FONT Color=gray>' ''Imposta la posizione dei restanti caratteri del testo da disegnare ruotato:''</font>
Riga 109: Riga 109:
 
       .Font.Bold = False
 
       .Font.Bold = False
 
       .Translate(60, 10)
 
       .Translate(60, 10)
       .Rotate(Rad(340))
+
<FONT Color=gray>' ''Aumentando questo valore, il cerchio del testo si allarga sino a diventare a 360° una linea dritta:''</font>
 +
       .Rotate(Rad(343))
 
       .Font.Size = 20
 
       .Font.Size = 20
 
       .Font.Bold = True
 
       .Font.Bold = True
 
       .Brush = .Color(Color.Red)
 
       .Brush = .Color(Color.Red)
       .DrawText(Chr(bb[b]), 0, 0, .Font.TextWidth(Chr(bb[b])), .Font.TextHeight(Chr(bb[b])), Align.Center)
+
       s = String.Mid(testo, b, 1)
 +
      .DrawText(s, 0, 0, .Font.TextWidth(s), .Font.TextHeight(s), Align.Center)
 
     Next
 
     Next
 
      
 
      

Versione delle 19:26, 3 giu 2021

Il caso è quello in cui si scrive un testo su una circonferenza ideale curvando ogni carattere in modo tale che il proprio asse verticale converga al centro di detta circonferenza. [Nota 1]
La rotazione è data dai valori presenti nei metodi "Paint.Traslate()" e "Paint.Rotate()" relativi alle misure dell'area di disegno e a quelle del testo.

Nell'esempio, che segue, si scriveranno alcuni caratteri lungo il semicerchio superiore della circonferenza:

Public Sub Form_Open()
 
 Dim DrawingArea1 As DrawingArea
 
 With Me
   .Center
   .W = 600
   .H = 500
   .Arrangement = Arrange.Fill
 End With
 
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 0
   .Y = 0
 End With
  
End

Public Sub DrawingArea1_Draw()

 With Paint
   .Arc(300, 300, 50, Rad(0), 360, False)
   .Stroke
   .DrawRect(250, 250, 50, 50, Color.Red, 1.0)
   .DrawRect(300, 250, 50, 50, Color.Red, 1.0)
   .DrawRect(250, 300, 50, 50, Color.Red, 1.0)
   .DrawRect(300, 300, 50, 50, Color.Red, 1.0)

   .Translate(240, 304)
   .Rotate(Rad(90))
   .Font.Bold = True
   .DrawText("A", 0, 0, 5, 5, Align.Left)
  
   .Font.Bold = False
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("B", 0, 0, 5, 5, Align.Left)
  
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("C", 0, 0, 5, 5, Align.Left)
  
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("D", 0, 0, 5, 5, Align.Left)
  
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("E", 0, 0, 5, 5, Align.Left)
  
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("F", 0, 0, 5, 5, Align.Left)
  
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("G", 0, 0, 5, 5, Align.Left)
  
   .Translate(44, 14)
   .Rotate(Rad(315))
   .DrawText("H", 0, 0, 5, 5, Align.Left)

   .End
 End With
 
End

Altro esempio, nel quale si consentirà di disegnare anche i caratteri speciali come le lettere accentate:

Public Sub Form_Open()
 
 Dim DrawingArea1 As DrawingArea
 
 With Me
   .Center
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With
 
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 0
   .Y = 0
 End With
  
End

Public Sub DrawingArea1_Draw()
 
 Dim b As Byte
 Dim s As String
 Dim testo As String = "Lì e là può di più."
 
 With Paint
' Imposta la posizione del primo carattere del testo da disegnare ruotato:
   .Translate(Me.W / 2.5, Me.H / 1.5)
   .Rotate(Rad(160))
   .Font.Size = 20
   .Font.Bold = True
   .Brush = .Color(Color.Red)
   s = String.Mid(testo, 1, 1)
   .DrawText(s, 0, 0, .Font.TextWidth(s), .Font.TextHeight(s), Align.Center)
  
' Imposta la posizione dei restanti caratteri del testo da disegnare ruotato:
   For b = 1 To bb.Max
     .Font.Bold = False
     .Translate(60, 10)
' Aumentando questo valore, il cerchio del testo si allarga sino a diventare a 360° una linea dritta: 
     .Rotate(Rad(343))
     .Font.Size = 20
     .Font.Bold = True
     .Brush = .Color(Color.Red)
     s = String.Mid(testo, b, 1)
     .DrawText(s, 0, 0, .Font.TextWidth(s), .Font.TextHeight(s), Align.Center)
   Next
   
   .End
 End With
 
End


Note

[1] Vedere anche questa pagina: Curvare la linea di testo in un oggetto Image