Voici comment realiser un progressbar Type XP.
vous pouvez changer de couleur texte,rectangle et aussi changer épaisseur du block entre les espaces.
ma classes :
Public Class ProgressbarStyleXP : Inherits ProgressBar
Private _Alignement As StringAlignment
Public Property TextAlignement As StringAlignment
Get
Return _Alignement
End Get
Set(value As StringAlignment)
_Alignement = value
MyBase.Invalidate()
End Set
End Property
Private _ColorText As Color
Public Property ColorText As Color
Get
Return _ColorText
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
_ColorText = value
MyBase.Invalidate()
End If
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
Private _BlockWidth As Integer = 6
Public Property BlockWidth As Integer
Get
Return Me._BlockWidth
End Get
Set(ByVal value As Integer)
If (Me._BlockWidth <> value) Then
Me._BlockWidth = If((value >= 1), If((value <= 20), value, 20), 1)
MyBase.Invalidate()
End If
End Set
End Property
Private _BlockSpace As Integer = 1
Public Property BlockSpace As Integer
Get
Return Me._BlockSpace
End Get
Set(ByVal value As Integer)
If (Me._BlockSpace <> value) Then
Me._BlockSpace = If((value >= 0), If((value <= 10), value, 10), 0)
MyBase.Invalidate()
End If
End Set
End Property
Private _MarginOffset As Boolean
Public Property MarginOffset As Boolean
Get
Return Me._MarginOffset
End Get
Set(ByVal value As Boolean)
If (Me._MarginOffset <> value) Then
Me._MarginOffset = value
MyBase.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
Public Sub New()
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
Me._BorderStyle = BorderStyle.Flat
Me._MarginOffset = True
Me._Color = Drawing.Color.Red
Me._MyFont = New Font("Times New Roman", 12, FontStyle.Regular)
Me._ColorText = Drawing.Color.Black
Me._Alignement = StringAlignment.Center
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Me.DrawPrgressBarBorder(e.Graphics)
Me.DrawProgressBar(e.Graphics)
MyBase.OnPaint(e)
End Sub
Public Enum BorderStyle
Flat = 0
SunkenOut = 1
SunkenInner = 2
Sunken = 3
[Single] = 4
None = 5
End Enum
Private Sub DrawPrgressBarBorder(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
Private Sub DrawProgressBar(g As Graphics)
Dim num As Decimal = (Me.Value / Me.Maximum)
Dim num3 As Integer = (Me._BlockWidth + Me._BlockSpace)
Dim num4 As Integer = ((CInt(((MyBase.ClientRectangle.Width - (Me.GetLeftOffSet * 2)) * num)) / num3) * num3)
If ((num > 0.99) AndAlso (((MyBase.ClientRectangle.Width - (Me.GetLeftOffSet * 2)) - num4) > 0)) Then
num4 = (num4 + (((MyBase.ClientRectangle.Width - (Me.GetLeftOffSet * 2)) - num4) / num3))
End If
Dim x As Integer = (MyBase.ClientRectangle.Left + Me.GetLeftOffSet)
Dim y As Integer = (MyBase.ClientRectangle.Top + Me.GetTopOffSet)
Dim height As Integer = (MyBase.ClientRectangle.Height - (Me.GetTopOffSet * 2))
Dim width As Integer = num3
Do While True
If (width > num4) Then
Dim num9 As Integer = ((MyBase.ClientRectangle.Width - x) - Me.GetLeftOffSet)
If ((num9 > 0) AndAlso (num9 < num3)) Then
width = ((MyBase.ClientRectangle.Width - x) - Me.GetLeftOffSet)
If (width > 0) Then
g.FillRectangle(New SolidBrush(_Color), x, y, width, height)
End If
End If
Return
End If
g.FillRectangle(New SolidBrush(_Color), x, y, Me._BlockWidth, height)
x = (x + num3)
width = (width + num3)
Using sf As New StringFormat With {.Alignment = _Alignement, .LineAlignment = StringAlignment.Center}
g.DrawString(Value & " %", _MyFont, New SolidBrush(_ColorText), Me.ClientRectangle, sf)
End Using
Loop
End Sub
Private Function GetLeftOffSet() As Integer
Return If(Me._MarginOffset, If(((Me._BorderStyle <> BorderStyle.Flat) AndAlso (Me._BorderStyle <> BorderStyle.Sunken)), If((Me._BorderStyle <> BorderStyle.None), 2, 1), 3), If((Me._BorderStyle <> BorderStyle.Flat), If((Me._BorderStyle <> BorderStyle.None), 1, 0), 2))
End Function
Private Function GetTopOffSet() As Integer
Return If(Me._MarginOffset, If(((Me._BorderStyle <> BorderStyle.Flat) AndAlso (Me._BorderStyle <> BorderStyle.Sunken)), If((Me._BorderStyle <> BorderStyle.None), 2, 1), 3), If(((Me._BorderStyle <> BorderStyle.Sunken) AndAlso (Me._BorderStyle <> BorderStyle.Flat)), If((Me._BorderStyle <> BorderStyle.None), 1, 0), 2))
End Function
End Class
Aucun commentaire:
Enregistrer un commentaire