Rechercher dans ce blog

mercredi 29 décembre 2021

Progressbar Rectangle Color

 Voici comment realiser un rectangle avec 4 couleur.


vous pouvez changer les 4 couleurs du rectangle et aussi fillrectangle aussi vous pouvez changer de couleur.

ma classe :

Public Class ProgressbarRectangleColor : Inherits ProgressBar

    Private _Color1 As Color
    Public Property Color1 As Color
        Get
            Return _Color1
        End Get
        Set(value As Color)
            If value = _Color2 Then
                Throw New ArgumentException("La Color1 ne peut pas étre la même couleur Color2.")
                Return
            ElseIf value = _Color3 Then
                Throw New ArgumentException("La Color1 ne peut pas étre la même couleur Color3.")
                Return
            ElseIf value = _Color4 Then
                Throw New ArgumentException("La Color1 ne peut pas étre la même couleur Color4.")
            Else
                _Color1 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _Color2 As Color
    Public Property Color2 As Color
        Get
            Return _Color2
        End Get
        Set(value As Color)
            If value = _Color1 Then
                Throw New ArgumentException("La Color2 ne peut pas étre la même couleur Color1.")
                Return
            ElseIf value = _Color3 Then
                Throw New ArgumentException("La Color2 ne peut pas étre la même couleur Color3.")
                Return
            ElseIf value = _Color4 Then
                Throw New ArgumentException("La Color2 ne peut pas étre la même couleur Color4.")
            Else
                _Color2 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _Color3 As Color
    Public Property Color3 As Color
        Get
            Return _Color3
        End Get
        Set(value As Color)
            If value = _Color1 Then
                Throw New ArgumentException("La Color3 ne peut pas étre la même couleur Color1.")
                Return
            ElseIf value = _Color2 Then
                Throw New ArgumentException("La Color3 ne peut pas étre la même couleur Color2.")
                Return
            ElseIf value = _Color4 Then
                Throw New ArgumentException("La Color3 ne peut pas étre la même couleur Color4.")
            Else
                _Color3 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _Color4 As Color
    Public Property Color4 As Color
        Get
            Return _Color4
        End Get
        Set(value As Color)
            If value = _Color1 Then
                Throw New ArgumentException("La Color4 ne peut pas étre la même couleur Color1.")
                Return
            ElseIf value = _Color2 Then
                Throw New ArgumentException("La Color4 ne peut pas étre la même couleur Color2.")
                Return
            ElseIf value = _Color3 Then
                Throw New ArgumentException("La Color4 ne peut pas étre la même couleur Color3.")
            Else
                _Color4 = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Private _Tickness As Single
    Public Property TicknessValue As Single
        Get
            Return _Tickness
        End Get
        Set(value As Single)
            If value < 1 OrElse value > 5 Then
                Throw New ArgumentException("La valeur maximum et minimum est entre 1 à 5 épaisseur.")
                Return
            Else
                _Tickness = value
                Me.Invalidate()
            End If
        End Set
    End Property

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

    Private _RectangleColor As Color
    Public Property RectangleColor As Color
        Get
            Return _RectangleColor
        End Get
        Set(value As Color)
            If value = _Color1 Then
                Throw New ArgumentException("La RectangleColor ne peut pas étre la même color1.")
                Return
            ElseIf value = _Color2 Then
                Throw New ArgumentException("La RectangleColor ne peut pas étre la même color2.")
                Return
            ElseIf value = _Color3 Then
                Throw New ArgumentException("La RectangleColor ne peut pas étre la même color3.")
                Return
            ElseIf value = _Color4 Then
                Throw New ArgumentException("La RectangleColor ne peut pas étre la même color4.")
                Return
            Else
                _RectangleColor = value
                Me.Invalidate()
            End If
        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.Size = New Size(200, 20)
        Me._Color1 = Color.Red
        Me._Color2 = Color.Green
        Me._Color3 = Color.Blue
        Me._Color4 = Color.Pink
        Me._Tickness = 5
        Me._MyFont = New Font("Times New Roman", 12, FontStyle.Regular)
        Me._ColorText = Color.Black
        Me._RectangleColor = Color.Yellow
    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
        rect.Width = ((Me.Width / (Me.Maximum - Me.Minimum)) * Value)
        If rect.Width < 0 Then Return
        e.Graphics.FillRectangle(New SolidBrush(_RectangleColor), rect)
        DrawRectengleColor(e.Graphics, rect, _Color1, _Color2, _Color3, _Color4, _Tickness)
        Using sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
            e.Graphics.DrawString(Value & " %", Me._MyFont, New SolidBrush(_ColorText), Me.ClientRectangle, sf)
        End Using
    End Sub

    Private Sub DrawRectengleColor(g As Graphics, rect As Rectangle, Color1 As Color, Color2 As Color, Color3 As Color, Color4 As Color, Tickness As Single)
        Using Pen1 As New Pen(Color1, Tickness)
            Pen1.Alignment = Drawing2D.PenAlignment.Center
            Pen1.LineJoin = Drawing2D.LineJoin.Bevel
            Pen1.SetLineCap(Drawing2D.LineCap.AnchorMask, Drawing2D.LineCap.AnchorMask, Drawing2D.DashCap.Triangle)
            g.DrawLine(Pen1, New Point(rect.X - 2, rect.Y + 1), New Point(rect.Width - 2, 1))
            If Value > 1 Then
                Pen1.Brush = New SolidBrush(Color2)
                g.DrawLine(Pen1, New Point(rect.Right - 2, rect.Y + 1), New Point(rect.Width - 2, rect.Height))
            End If
            Pen1.Brush = New SolidBrush(Color3)
            g.DrawLine(Pen1, New Point(rect.X - 2, rect.Bottom - 2), New Point(rect.Width - 2, rect.Height - 2))
            If Value > 0 Then
                Pen1.Brush = New SolidBrush(Color4)
                g.DrawLine(Pen1, New Point(rect.X + 1, rect.Y), New Point(rect.Left + 1, rect.Height))
            End If
        End Using
    End Sub

End Class

Aucun commentaire:

Enregistrer un commentaire