Rechercher dans ce blog

mercredi 29 décembre 2021

Progressbar Mutiple colors

 Voici comment réaliser un progressbar multiplie couleur.


Vous pouvez changer type de couleur. vous pouvez choisir une couleur ou trois couleur ou couleur raimbow et aussi choisir type de texte.

ma classe:

Imports System.Drawing.Drawing2D

Class MyProgressBarMutipleColor
    Inherits ProgressBar

    Private ReadOnly Property _percentageStr As String
        Get
            Return String.Format("{0} %", CInt((CSng(Value) - Minimum)) / (CSng(Maximum) - Minimum) * 100)
        End Get
    End Property

    Private ReadOnly Property _currProgressStr As String
        Get
            Return String.Format("{0} / {1}", Value, Maximum)
        End Get
    End Property

    Private _text As String = String.Empty
    Public Property CustomText As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
            Invalidate()
        End Set
    End Property

    Private _visualMode As ProgressBarDisplayMode = ProgressBarDisplayMode.CurrProgress
    Public Property CustomVisualMode As ProgressBarDisplayMode
        Get
            Return _visualMode
        End Get
        Set(ByVal value As ProgressBarDisplayMode)
            _visualMode = value
            Invalidate()
        End Set
    End Property
    Private Property _textToDraw As String
        Get
            Dim text As String = CustomText

            Select Case CustomVisualMode
                Case (ProgressBarDisplayMode.Percentage)
                    text = _percentageStr
                Case (ProgressBarDisplayMode.CurrProgress)
                    text = _currProgressStr
                Case (ProgressBarDisplayMode.TextAndCurrProgress)
                    text = String.Format("{0} : {1}", CustomText, _currProgressStr)
                Case (ProgressBarDisplayMode.TextAndPercentage)
                    text = String.Format("{0} : {1}", CustomText, _percentageStr)
            End Select
            Return text
        End Get
        Set(ByVal value As String)
        End Set
    End Property

    Dim _SimpleColor As Color = Color.Red
    Property ColorSimple1 As Color
        Get
            Return _SimpleColor
        End Get
        Set(value As Color)
            _SimpleColor = value
            Me.Invalidate()
        End Set
    End Property

    Dim _ColorSimpleA As Boolean
    Property ColorSimple As Boolean
        Get
            Return _ColorSimpleA
        End Get
        Set(value As Boolean)
            _ColorSimpleA = value
            If _ColorSimpleA = True Then
                _3Color = False
                _ColorMultiple = False
            End If
        End Set
    End Property

    Dim _3Color As Boolean
    Property ColorM3 As Boolean
        Get
            Return _3Color
        End Get
        Set(value As Boolean)
            _3Color = value
            If _3Color = True Then
                _ColorMultiple = False
                _ColorSimpleA = False
            End If
        End Set
    End Property

    Dim _ColorMultiple As Boolean
    Property ColorMutiple As Boolean
        Get
            Return _ColorMultiple
        End Get
        Set(value As Boolean)
            _ColorMultiple = value
            If _ColorMultiple = True Then
                _3Color = False
                _ColorSimpleA = False
            End If
        End Set
    End Property

    Dim _Position As Single = 0.8
    Property ColorPosition() As Single
        Get
            Return _Position
        End Get
        Set(value As Single)
            If _Position < 0.1 OrElse _Position > 0.9 Then
                Throw New ArgumentException("la valeur doit être compris entre 0.1 et 0.9")
                Return
            Else
                _Position = 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)
            _color3 = value
            Me.Invalidate()
        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)
            _color2 = value
            Me.Invalidate()
        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)
            _color1 = value
            Me.Invalidate()
        End Set
    End Property

    Dim _colorText As Color = Drawing.Color.Black
    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 = New Font("Microsoft Sans Serif", 8, FontStyle.Regular)
    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.BackColor = SystemColors.Control
        Me._visualMode = ProgressBarDisplayMode.Percentage
        Me.ColorMutiple = True
        Me.Size = New Size(250, 23)
    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.Blue, Color.White, LinearGradientMode.Horizontal)
        Dim ColorBlend As New ColorBlend()
        If _ColorMultiple = True Then
            ColorBlend.Colors = New Color() {Color.FromArgb(255, 255, 0, 127), Color.FromArgb(255, 255, 0, 0), Color.FromArgb(255, 255, 255, 0), Color.FromArgb(255, 0, 255, 0), Color.FromArgb(255, 0, 255, 255), Color.FromArgb(255, 0, 0, 255), Color.FromArgb(255, 127, 0, 255)}
            ColorBlend.Positions = New Single() {0, 0.1F, 0.3F, 0.5F, 0.7F, 0.9F, 1}
            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))
        ElseIf _3Color = True Then
            ColorBlend.Colors = New Color() {_color1, _color2, _color3}
            ColorBlend.Positions = New Single() {0.0, _Position, 1}
            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))
        ElseIf _ColorSimpleA = True Then
            Dim ProgressWidth2 As Integer = CInt((Me.Width / (Me.Maximum - Me.Minimum)) * Me.Value)
            e.Graphics.FillRectangle(New SolidBrush(_SimpleColor), New Rectangle(0, 0, ProgressWidth2, Me.Height))
        End If
        DrawStringIfNeeded(e.Graphics)
        MyBase.OnPaint(e)
    End Sub

    Private Sub DrawStringIfNeeded(ByVal g As Graphics)
        If CustomVisualMode <> ProgressBarDisplayMode.NoText Then
            Dim text As String = _textToDraw
            Dim len As SizeF = g.MeasureString(text, MyFont)
            Dim location As Point = New Point(((Width / 2) - CInt(len.Width) / 2), ((Height / 2) - CInt(len.Height) / 2))
            g.DrawString(text, MyFont, New SolidBrush(_colorText), location)
        End If
    End Sub

    Public Enum ProgressBarDisplayMode
        NoText
        Percentage
        CurrProgress
        CustomText
        TextAndPercentage
        TextAndCurrProgress
    End Enum
End Class

Aucun commentaire:

Enregistrer un commentaire