Inserire un testo in diagonale in un Controllo grafico

Da Gambas-it.org - Wikipedia.

Di seguito mostriamo un possibile codice per inserire un testo in diagonale in un Controllo grafico, ad esempio in una DrawingArea:

Private DrawingArea1 As DrawingArea


Public Sub Form_Open()

 With Me
   .W = 400
   .H = 400
   .Center
   .Arrangement = Arrange.Fill
 End With
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 0
   .Y = 0
   .Background = Color.Orange
 End With

End


Public Sub DrawingArea1_Draw()

 With Paint
   .LineTo(400, 400)
   .Stroke
' --- il testo ---
   .Translate(140, 100)
   .Rotate(Rad(315))
   .Font.Size = 20
   .Brush = .Color(Color.Yellow)
   .DrawText("G", 0, 0, .Font.TextWidth("G"), .Font.TextHeight("G"), Align.Center)
   .DrawText("a", 20, 0, .Font.TextWidth("a"), .Font.TextHeight("a"), Align.Center)
   .DrawText("m ", 40, 0, .Font.TextWidth("m"), .Font.TextHeight("m"), Align.Center)
   .DrawText("b", 65, 0, .Font.TextWidth("b"), .Font.TextHeight("b"), Align.Center)
   .DrawText("a", 83, 0, .Font.TextWidth("a"), .Font.TextHeight("a"), Align.Center)
   .DrawText("s", 100, 0, .Font.TextWidth("s"), .Font.TextHeight("s"), Align.Center)
   .End
 End With

End

o più semplicemente: <SUP[nota 1]</sup>

Private DrawingArea1 As DrawingArea
Private Slider1 As Slider


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth * 0.5
   .H = Screen.AvailableHeight * 0.5
   .Center
 End With
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 0
    .Y = 0
   .W = Me.W * 0.8
   .H = Me.H
   .Background = Color.Orange
 End With
 With Slider1 = New Slider(Me) As "Slider1"
   .X = Me.W * 0.85
   .Y = Me.H * 0.02
   .W = Me.W * 0.1
   .H = Me.H * 0.95
   .MinValue = 0
   .MaxValue = 360
 End With
 
End


Public Sub DrawingArea1_Draw()

 Dim testo As String = "Gambas " & CStr(Slider1.Value) & "°"

 With Paint
' '  Impone la rotazione del testo, disegnato, sempre al centro della "DrawingARea":
   .Translate(DrawingArea1.W / 2, DrawingArea1.H / 2)
   .Rotate(Rad(-Slider1.Value))
   .Brush = Paint.Color(Color.Yellow)
   .Font = Font["Sans Serif, 22"]
' Il testo ruoterà comunque intorno al suo centro verticale e orizzontale:
   .DrawText(testo, -Paint.TextExtents(testo).W / 2, Paint.TextExtents(testo).H / 2, -.TextExtents(testo).W / 2, -.TextExtents(testo).H / 2, Align.Center)
' oppure anche così:
'  .DrawText(testo, -.Font.TextWidth(testo) / 2, -.Font.TextHeight(testo) / 2, .Font.TextWidth(testo), .Font.TextHeight(testo), Align.Center)
   .End
 End With

End


Public Sub Slider1_Change()

 DrawingArea1.Refresh

End


Note

[1] Vedere anche le seguenti pagine: