Rechercher dans ce blog

mercredi 29 décembre 2021

Progressbar 3 ou 4 color

 voici comment realiser un progressbar avec 3 ou 4 couleur.



vous pouvez choisir entre 3 ou 4 couleur et aussi changer les couleurs.

ma classe :

Imports System.Drawing.Drawing2D

Public Class ProgressbarColor : Inherits ProgressBar

    Dim _color4Progressbar As Boolean = False
    Public Property Color4Progressbar As Boolean
        Get
            Return _color4Progressbar
        End Get
        Set(value As Boolean)
            If value = True Then
                _color4Progressbar = value
                _Color3ProgressBar = False
            Else
                _color4Progressbar = False
                _Color3ProgressBar = True
            End If
            Me.Invalidate()
        End Set
    End Property

    Dim _Color3ProgressBar As Boolean = True
    Public Property Color3Progressbar As Boolean
        Get
            Return _Color3ProgressBar
        End Get
        Set(value As Boolean)
            If value = True Then
                _Color3ProgressBar = value
                _color4Progressbar = False
            Else
                _Color3ProgressBar = False
                _color4Progressbar = True
            End If
            Me.Invalidate()
        End Set
    End Property

    Dim _color4 As Color = Color.Blue
    Property Color4() As Color
        Get
            Return _color4
        End Get
        Set(ByVal value As Color)
            If value = _color1 Then
                Throw New ArgumentException("la couleur 4 ne peut pas étre la méme couleur 1.")
                Return
            ElseIf value = _color2 Then
                Throw New ArgumentException("la couleur 4 ne peut pas étre la méme couleur 2.")
                Return
            ElseIf value = _color3 Then
                Throw New ArgumentException("la couleur 4 ne peut pas étre la méme couleur 3.")
                Return
            Else
                _color4 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Dim _color3 As Color = Drawing.Color.Red
    Property Color3() As Color
        Get
            Return _color3
        End Get
        Set(ByVal value As Color)
            If value = _color1 Then
                Throw New ArgumentException("la couleur 3 ne peut pas étre la méme couleur 1.")
                Return
            ElseIf value = _color2 Then
                Throw New ArgumentException("la couleur 3 ne peut pas étre la méme couleur 2.")
                Return
            ElseIf value = _color4 Then
                Throw New ArgumentException("la couleur 3 ne peut pas étre la méme couleur 4.")
                Return
            Else
                _color3 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Dim _color2 As Color = Drawing.Color.Orange
    Property Color2() As Color
        Get
            Return _color2
        End Get
        Set(ByVal value As Color)
            If value = _color1 Then
                Throw New ArgumentException("la couleur 2 ne peut pas étre la méme couleur 1.")
                Return
            ElseIf value = _color3 Then
                Throw New ArgumentException("la couleur 2 ne peut pas étre la méme couleur 3.")
                Return
            ElseIf value = _color4 Then
                Throw New ArgumentException("la couleur 2 ne peut pas étre la méme couleur 4.")
                Return
            Else
                _color2 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Dim _color1 As Color = Drawing.Color.Lime
    Property Color1() As Color
        Get
            Return _color1
        End Get
        Set(ByVal value As Color)
            If value = _color2 Then
                Throw New ArgumentException("la couleur 1 ne peut pas étre la méme couleur 2.")
                Return
            ElseIf value = _color3 Then
                Throw New ArgumentException("la couleur 1 ne peut pas étre la méme couleur 3.")
                Return
            ElseIf value = _color4 Then
                Throw New ArgumentException("la couleur 1 ne peut pas étre la méme couleur 4.")
                Return
            Else
                _color1 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    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.Size = New Size(250, 23)
        Me._colorText = Color.Black
        Me._font = New Font("Microsoft Sans Serif", 8, FontStyle.Regular)
    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
        Dim br As New LinearGradientBrush(rect, Color.White, Color.White, LinearGradientMode.Horizontal)
        Dim ColorBlend As New ColorBlend()
        If _Color3ProgressBar Then
            ColorBlend.Colors = New Color() {_color1, _color1, _color2, _color2, _color3, _color3}
            ColorBlend.Positions = New Single() {0.0, 0.5, 0.5, 0.8, 0.8, 1}
        ElseIf _color4Progressbar Then
            ColorBlend.Colors = New Color() {_color1, _color1, _color2, _color2, _color3, _color3, Color4, Color4}
            ColorBlend.Positions = New Single() {0.0, 0.4, 0.4, 0.6, 0.6, 0.8, 0.8, 1}
        End If
        br.InterpolationColors = ColorBlend
        Dim ProgressWidth As Integer = CInt((Me.Width / (Me.Maximum - Me.Minimum)) * Me.Value)
        e.Graphics.FillRectangle(br, 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)
    End Sub

Aucun commentaire:

Enregistrer un commentaire