VB.NET - COME CAMBIARE IL COLORE DI UNA ProgressBar (Codice)

Oggi vi parlo di come personalizzare la ProgressBar con colori diversi e colorare il testo di Percentuale che visualizzate al centro della ProgressBar.
Per prima cosa creo un file e ci copio il seguente codice :
CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET: : (File : ProgressBarNewStyle.vb)
Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Namespace ProgressBarNewStyle Class MyProgressBarStyle Inherits ProgressBar Public ProgressBarLabelValue As String = "0" Public colorText As Color = Color.Green Private obbligateColor As Color Private colorBarBottom As SolidBrush Private colorBarTop As SolidBrush Public Sub New() Me.SetStyle(ControlStyles.UserPaint, False) End Sub Public Sub New(colorBar As Color) Me.SetStyle(ControlStyles.UserPaint, True) Me.obbligateColor = Color.White Me.colorBarBottom = New SolidBrush(colorBar) 'riempimento del "rettangolo" superiore bianco con opacità del 50% Me.colorBarTop = New SolidBrush(Color.FromArgb(128, obbligateColor)) End Sub Protected Overrides Sub OnPaint(e As PaintEventArgs) Dim width As Integer = 0 Dim height As Integer = 0 'La proprietà ClipRectangle applicata su PaintEventArgs ritorna il rettangolo che bisogna riempire Dim rec As Rectangle = e.ClipRectangle 'Calcolo le dimensioni del rettangolo -4 rec.Width = CInt(Math.Truncate(rec.Width * (CDbl(Value) / Maximum))) - 4 rec.Height = rec.Height - 4 'grandezza e altezza del rettangolo con opacità che andremmo a sovrappore su quello principale per 'generare l'effetto width = rec.Width height = (rec.Height \ 2) 'ProgressBarRender.isSupported verifica se può essere effettuato il rendering del rettangolo in questione 'nella progressBar con la visualizzazione corrente del sistema operativo in uso If ProgressBarRenderer.IsSupported Then 'uso un metodo statico su questa classe per disegnare orizzontalmente nel componente 'ovviamente la progressBar verrà svuotata per fare spazio al nostro riempimento ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle) End If 'riempio l'interno dei rettangoli ottenuti e.Graphics.FillRectangle(colorBarBottom, 2, 2, rec.Width, rec.Height) e.Graphics.FillRectangle(colorBarTop, 2, 2, width, height) 'Applico una scritta Using sb As New SolidBrush(colorText) Dim sz As SizeF = e.Graphics.MeasureString(ProgressBarLabelValue + " %", Me.Font) e.Graphics.DrawString(ProgressBarLabelValue + " %", Me.Font, sb, New PointF((Me.Width - sz.Width) / 2.0F, (Me.Height - sz.Height) / 2.0F)) End Using End Sub End Class End Namespace
Codice che inserisco nel Form o dove voglio far apparire la ProgressBar :
CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET: : (File : Form1.vb)
Public Class Form1 Private newProgressBar As New ProgressBarNewStyle.MyProgressBarStyle(Color.Violet) Private newProgressBar1 As New ProgressBarNewStyle.MyProgressBarStyle(Color.GreenYellow) Public Sub New() InitializeComponent() newProgressBar.Location = New Point(12, 12) newProgressBar.Width = 200 newProgressBar.colorText = Color.Black Me.Controls.Add(newProgressBar) newProgressBar1.Location = New Point(12, 50) newProgressBar1.Width = 276 newProgressBar1.colorText = Color.Tomato Me.Controls.Add(newProgressBar1) End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click newProgressBar.Style = ProgressBarStyle.Blocks If newProgressBar.Value < newProgressBar.Maximum Then newProgressBar.ProgressBarLabelValue = newProgressBar.Value.ToString newProgressBar.Value += 10 End If End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click newProgressBar1.Style = ProgressBarStyle.Blocks If newProgressBar1.Value < newProgressBar1.Maximum Then newProgressBar1.Value += 10 newProgressBar1.ProgressBarLabelValue = newProgressBar1.Value.ToString End If End Sub End Class
Potete scaricare il file d'esempio :
Password : "ImaginSystem"
Link File Download : Download Personalizzare il colore di una ProgressBar , Style ProgressBar
By ImaginSystems & Queen Gin