Simulare una ProgressBar mediante una DrawingArea

Da Gambas-it.org - Wikipedia.

La simulazione di una "ProgressBar" mediante l'uso di una "DrawingArea", può essere utile, ad esempio, per utilizzare un colore diverso da quello standard fornito dal tema grafico.

Mostriamo di seguito una "DrawingArea" che simula il comportamento di un Oggetto "ProgressBar", ma di colore giallo:

Private DrawingArea1 As DrawingArea
Private dw As Short
Private dh As Short
Private valore As Single
Private tempus As Timer


Public Sub Form_Open()

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 10
   .Y = 10
   .W = 300
   .H = 50
   .Background = Color.Red
   dw = .W / 2
   dh = .H / 2
 End With

 With tempus = New Timer As "Tempus"
   .Delay = 40
   .Start
 End With

End


Public Sub Tempus_Timer()
 
 If (valore * 100) / DrawingArea1.W >= 100.0 Then tempus.Stop
 valore += 1.0
 DrawingArea1.Refresh

End


Public Sub DrawingArea1_Draw()

 With Paint
' Imposta il colore giallo per la "ProgressBar" simulata:
   .Brush = .Color(Color.Yellow)
   .Rectangle(0, 0, valore, DrawingArea1.H)
   .Fill 
   .Brush = .Color(Color.Blue)
   .DrawText(CStr(Fix((valore * 100) / DrawingArea1.W)) & "%", dw - (.TextExtents(CStr(valore)).W / 2),
             dh - (.TextExtents(CStr(valore)).H / 2), .TextExtents(CStr(valore)).W, .TextExtents(CStr(valore)).H, Align.Normal)
   .End
 End With

End

Nel codice seguente invece si darà un effetto arcobaleno:

Private DrawingArea1 As DrawingArea
Private dw As Short
Private dh As Short
Private valore As Single
Private tempus As Timer


Public Sub Form_Open()

 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 10
   .Y = 10
   .W = 300
   .H = 50
   .Background = Color.LightGray
   dw = .W / 2
   dh = .H / 2
 End With

 With tempus = New Timer As "Tempus"
   .Delay = 40
   .Start
 End With

End


Public Sub Tempus_Timer()
 
 If (valore * 100) / DrawingArea1.W >= 100.0 Then tempus.Stop
 valore += 1.0
 DrawingArea1.Refresh

End


Public Sub DrawingArea1_Draw()

 Dim c As Integer[] = [Color.Blue, Color.Green, Color.Yellow, Color.Red]
 Dim f As Float[] = [0, 0.34, 0.67, 1]

 With Paint
' Imposta un motivo arcobaleno orizzontale per la "ProgressBar" simulata:
   .Brush = .LinearGradient(valore, 0, valore, DrawingArea1.H, c, f)
   .Rectangle(0, 0, valore, DrawingArea1.H)
   .Fill 
   .Brush = .Color(Color.Black)
   .DrawText(CStr(Fix((valore * 100) / DrawingArea1.W)) & "%", dw - (.TextExtents(CStr(valore)).W / 2),
             dh - (.TextExtents(CStr(valore)).H / 2), .TextExtents(CStr(valore)).W, .TextExtents(CStr(valore)).H, Align.Normal)
   .End
 End With

End