Rechercher dans ce blog

mercredi 29 décembre 2021

Progressbar classic

 Voici comment réaliser un progressbar avec un pourcentage au centre.


Vous pouvez changer de couleur du pourcentage et change la taille du texte.

ma classes :

Class MyProgressBar : Inherits ProgressBar

    Dim _colorText As Color
    Property colorText() As Color
        Get
            Return _colorText
        End Get
        Set(ByVal value As Color)
            _colorText = value
            Me.Invalidate()
        End Set
    End Property

    Dim _font As Font
    Property MyFont() As Font
        Get
            Return _font
        End Get
        Set(value As Font)
            _font = value
            Me.Invalidate()
        End Set
    End Property

    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
        Me._colorText = Drawing.Color.Blue
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        ProgressBarRenderer.DrawHorizontalBar(e.Graphics, New Rectangle(0, 0, Me.Width, Me.Height))
        Dim ProgressWidth As Integer = CInt((Me.Width / (Me.Maximum - Me.Minimum)) * Me.Value)
        ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, New Rectangle(0, 0, ProgressWidth, Me.Height))
        Dim ProgressPercent As Integer = CInt(((Me.Maximum - Me.Minimum) / 100) * Me.Value)
        TextRenderer.DrawText(e.Graphics, ProgressPercent.ToString & "%", _font, New Rectangle(0, 0, Me.Width, Me.Height), _colorText, TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
        MyBase.OnPaint(e)
    End Sub

Aucun commentaire:

Enregistrer un commentaire