Rechercher dans ce blog

mercredi 29 décembre 2021

Progressbar Classic

 Voici comment réaliser un progressbar classic.


vous pouvez choisir une couleur ou 3 couleur et aussi changer les couleurs et aussi choisir la taille rectangle et épaisseur entre et aussi choisir type drawborder progressbar.

ma classe :

Imports System.Drawing.Drawing2D

Public Class ProgressbarClassic : Inherits ProgressBar

    Private _Color3 As Color
    Public Property Color3 As Color
        Get
            Return _Color3
        End Get
        Set(value As Color)
            If value = Drawing.Color.Transparent OrElse _Color2 = value OrElse _Color3 = value Then
                Return
            Else
                _Color1 = value
                MyBase.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 = Drawing.Color.Transparent OrElse _Color1 = value OrElse _Color3 = value Then
                Return
            Else
                _Color2 = value
                MyBase.Invalidate()
            End If
        End Set
    End Property

    Private _Color1 As Color
    Public Property Color1 As Color
        Get
            Return _Color1
        End Get
        Set(value As Color)
            If value = Drawing.Color.Transparent OrElse _Color2 = value OrElse _Color3 = value Then
                Return
            Else
                _Color1 = value
                MyBase.Invalidate()
            End If
        End Set
    End Property

    Private _ColorB3 As Boolean
    Public Property ColorB3 As Boolean
        Get
            Return _ColorB3
        End Get
        Set(value As Boolean)
            _ColorB3 = value
            MyBase.Invalidate()
        End Set
    End Property

    Private _Color As Color
    Public Property Color As Color
        Get
            Return _Color
        End Get
        Set(value As Color)
            If value = Drawing.Color.Transparent Then
                Throw New ArgumentException("La couleur transparent ne peut pas étre sélectionné.")
                Return
            Else
                _Color = value
                MyBase.Invalidate()
            End If
        End Set
    End Property

    Private _BorderStyle As BorderStyle
    Public Property ProgressBarBoderStyle As BorderStyle
        Get
            Return Me._BorderStyle
        End Get
        Set(ByVal value As BorderStyle)
            If (Me._BorderStyle <> value) Then
                Me._BorderStyle = value
                MyBase.Invalidate()
            End If
        End Set
    End Property

    Public _BlockWidth As Integer = 10
    Public Property BlockWidth As Integer
        Get
            Return _BlockWidth
        End Get
        Set(value As Integer)
            If value < 10 OrElse value > 20 Then
                Throw New ArgumentException("La valeur BlockWidth est entre 10 à 20 maximum")
                Return
            Else
                _BlockWidth = value
                MyBase.Invalidate()
            End If
        End Set
    End Property

    Public _BlockSpace As Integer = 1
    Public Property BlockSpace As Integer
        Get
            Return _BlockSpace
        End Get
        Set(value As Integer)
            If value < 1 OrElse value > 4 Then
                Throw New ArgumentException("la valeur blockspace est entre 1 à 4 maximum")
                Return
            Else
                _BlockSpace = value
                MyBase.Invalidate()
            End If
        End Set
    End Property

    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
        Me._BorderStyle = BorderStyle.Flat
        Me._Color = Drawing.Color.Green
        Me.Size = New Size(340, 23)
        Me._BlockSpace = 2
        Me._BlockWidth = 15
        Me._ColorB3 = False
        Me._Color1 = Drawing.Color.Green
        Me._Color2 = Drawing.Color.Orange
        Me._Color3 = Drawing.Color.Red
    End Sub

    Protected Overrides Sub OnResize(ByVal e As EventArgs)
        MyBase.OnResize(e)
        If Me.Height <> 23 Then
            Me.Height = 23
        ElseIf Me.Width <> 340 Then
            Me.Width = 340
        End If
    End Sub


    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim ProgressBarRect As New Rectangle(0, 0, _BlockWidth, MyBase.Height - 4)
        Dim PercentDone As Single = CSng(Value / Maximum)
        Dim valueLength As Single = PercentDone * CSng(MyBase.Width)
        Dim BlockCount As Integer = CInt(valueLength / (_BlockWidth + _BlockSpace) + 0.5)
        ProgressBarRect.Offset(2, 2)
        For i As Integer = 0 To BlockCount - 1
            If _ColorB3 Then
                Dim br As New LinearGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), Color.Transparent, Color.Transparent, LinearGradientMode.Horizontal)
                Dim ColorBlend As New ColorBlend()
                ColorBlend.Colors = New Color() {_Color1, _Color1, _Color2, _Color2, _Color3, _Color3}
                ColorBlend.Positions = New Single() {0, 0.4, 0.4, 0.8, 0.8, 1}
                br.InterpolationColors = ColorBlend
                e.Graphics.FillRectangle(br, ProgressBarRect)
            Else
                e.Graphics.FillRectangle(New SolidBrush(_Color), ProgressBarRect)
            End If
            ProgressBarRect.Offset(_BlockWidth + _BlockSpace, 0)
        Next
        DrawProgressBarBorder(e.Graphics)
    End Sub

    Private Sub DrawProgressBarBorder(g As Graphics)
        If (Me._BorderStyle = BorderStyle.Single) Then
            ControlPaint.DrawBorder(g, MyBase.ClientRectangle, Color.Black, ButtonBorderStyle.Solid)
        ElseIf (Me._BorderStyle = BorderStyle.Flat) Then
            ControlPaint.DrawBorder3D(g, MyBase.ClientRectangle, Border3DStyle.Flat)
        ElseIf (Me._BorderStyle = BorderStyle.Sunken) Then
            ControlPaint.DrawBorder3D(g, MyBase.ClientRectangle, Border3DStyle.Sunken)
        ElseIf (Me._BorderStyle = BorderStyle.SunkenInner) Then
            ControlPaint.DrawBorder3D(g, MyBase.ClientRectangle, Border3DStyle.SunkenInner)
        ElseIf (Me._BorderStyle = BorderStyle.SunkenOut) Then
            ControlPaint.DrawBorder3D(g, MyBase.ClientRectangle, Border3DStyle.SunkenOuter)
        End If
    End Sub

    Public Enum BorderStyle
        Flat = 0
        SunkenOut = 1
        SunkenInner = 2
        Sunken = 3
        [Single] = 4
        None = 5
    End Enum
End Class

Aucun commentaire:

Enregistrer un commentaire