Rechercher dans ce blog

mercredi 29 décembre 2021

Progressbar Translucent

 voici comment réaliser un progressbar translucent.


vous pouvez aussi choisir le texte centrer ou pas et aussi choisir la couleur dans backgroundcolor1 et backgroundcolor2.

ma classes :

Public Class MyProgressbarTranslucent : Inherits ProgressBar

    Private _AligementCenter As Boolean
    Public Property AlignementCenter As Boolean
        Get
            Return _AligementCenter
        End Get
        Set(value As Boolean)
            _AligementCenter = value
            Me.Invalidate()
        End Set
    End Property

    Private _BargroundColor As Color
    Public Property BargroundColor As Color
        Get
            Return _BargroundColor
        End Get
        Set(value As Color)
            If value = _BargroundColor2 Then
                Throw New ArgumentException("la couleur 1 ne doit pas étre la méme que la couleur 2")
            Else
                _BargroundColor = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _BargroundColor2 As Color = Color.Lime
    Public Property BargroundColor2 As Color
        Get
            Return _BargroundColor2
        End Get
        Set(value As Color)
            If value = _BargroundColor Then
                Throw New ArgumentException("la couleur 2 ne doit pas étre la méme que la couleur 1")
            Else
                _BargroundColor2 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _Font As Font
    Public Property MyFont As Font
        Get
            Return _Font
        End Get
        Set(value As Font)
            _Font = value
            Me.Invalidate()
        End Set
    End Property

    Private _ColorText As Color
    Public Property ColorText As Color
        Get
            Return _ColorText
        End Get
        Set(value As Color)
            _ColorText = value
            Me.Invalidate()
        End Set
    End Property

    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
        Me._BargroundColor = Color.Red
        Me.ColorText = Color.Blue
        Me._Font = New Font("Times New Roman", 10, FontStyle.Regular)
        Me._AligementCenter = True
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        If ProgressBarRenderer.IsSupported Then
            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rect)
        End If
        e.Graphics.FillRectangle(New SolidBrush(_BargroundColor), New Rectangle(0, 0, Me.Width, Me.Height))
        rect.Width = CInt((rect.Width / (Me.Maximum - Me.Minimum) * Me.Value))
        If rect.Width < 0 Then Return
        Using b As New SolidBrush(_BargroundColor2)
            e.Graphics.FillRectangle(b, rect)
        End Using
        Using sf As StringFormat = New StringFormat()
            If _AligementCenter Then
                sf.Alignment = StringAlignment.Center
                sf.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString(Value & " %", Me._Font, New SolidBrush(_ColorText), Me.ClientRectangle, sf)
            Else
                If Value > 6 Then
                    sf.Alignment = StringAlignment.Far
                    sf.LineAlignment = StringAlignment.Center
                    e.Graphics.DrawString(Value & "%", Me._Font, New SolidBrush(_ColorText), New Rectangle(0, 0, rect.Width, rect.Height), sf)
                Else
                    sf.Alignment = StringAlignment.Far
                    sf.LineAlignment = StringAlignment.Center
                    e.Graphics.DrawString(Value & "%", Me._Font, New SolidBrush(_ColorText), New Rectangle(0, 0, rect.Width + _Font.Size + 15, rect.Height), sf)
                End If
            End If
        End Using
    End Sub


End Class

Aucun commentaire:

Enregistrer un commentaire