Voici comment realiser un progressbar Pacman animé.
Vous pouvez changer de couleur du pacman et aussi drawborder et les cercles.
mon code :
Public Class ProgressbarPacMan : Inherits ProgressBar
Private _ColorFill As Color = Color.Black
Public Property ColorFill As Color
Get
Return _ColorFill
End Get
Set(value As Color)
If value = Color.Transparent OrElse value = _ColorCercle OrElse value = _ColorPacman Then
Return
Else
_ColorFill = value
Me.Invalidate()
End If
End Set
End Property
Private _ColorCercle As Color = Color.White
Public Property ColorCercle As Color
Get
Return _ColorCercle
End Get
Set(value As Color)
If value = Color.Transparent OrElse value = Color.Black Then
Return
Else
_ColorCercle = value
Me.Invalidate()
End If
End Set
End Property
Private _ColorPacman As Color = Color.Yellow
Public Property ColorPacman As Color
Get
Return _ColorPacman
End Get
Set(value As Color)
If value = Color.Transparent OrElse value = Color.Black Then
Return
Else
_ColorPacman = value
Me.Invalidate()
End If
End Set
End Property
Private _ColorDraw As Color = Color.Blue
Public Property ColorDraw As Color
Get
Return _ColorDraw
End Get
Set(value As Color)
If value = Color.Transparent Then
Return
Else
_ColorDraw = value
Me.Invalidate()
End If
End Set
End Property
Private rect As New Rectangle
Private timer As New Timer With {.Enabled = True, .Interval = 125}
Private counter As Integer = 0
Private moving As Boolean = False
Public Sub New()
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
Me.Size = New Size(300, 30)
Me.counter = 0
AddHandler timer.Tick, New EventHandler(AddressOf timer_Tick)
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
rect = New Rectangle(0, 0, Me.Width, Me.Height)
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
DrawRectangle(e.Graphics, _ColorDraw, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3, 6)
FillRectangle(e.Graphics, _ColorFill, rect.X + 2, rect.Y + 2, rect.Width - 5, rect.Height - 5, 6)
Dim progressbarWidth As Integer = CInt(((rect.Width - 35) / (Maximum - Minimum)) * Value)
GETCirleColor(e.Graphics, _ColorFill, _ColorCercle, rect, progressbarWidth)
PacMan(e.Graphics, counter, rect, progressbarWidth, _ColorPacman)
End Sub
Private Sub DrawRectangle(ByVal g As Graphics, ByVal _Colors As Color, ByVal x As Int32, ByVal y As Int32, ByVal width As Int32, ByVal height As Int32, ByVal radius As Int32)
Dim area As Rectangle = New Rectangle(x, y, width, height)
Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath
path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90)
path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90)
path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90)
path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90)
path.CloseAllFigures()
g.DrawPath(New Pen(_Colors, 3), path)
End Sub
Private Sub FillRectangle(g As Graphics, _colors As Color, x As Integer, y As Integer, Width As Integer, Height As Integer, Radius As Integer)
Dim area As Rectangle = New Rectangle(x, y, Width, Height)
Dim path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath
path.AddArc(area.Left, area.Top, Radius * 2, Radius * 2, 180, 90)
path.AddArc(area.Right - (Radius * 2), area.Top, Radius * 2, Radius * 2, 270, 90)
path.AddArc(area.Right - (Radius * 2), area.Bottom - (Radius * 2), Radius * 2, Radius * 2, 0, 90)
path.AddArc(area.Left, area.Bottom - (Radius * 2), Radius * 2, Radius * 2, 90, 90)
path.CloseAllFigures()
g.FillPath(New SolidBrush(_colors), path)
End Sub
Private Sub timer_Tick(sender As System.Object, e As System.EventArgs)
If Value = Minimum Then
counter = 0
ElseIf Value > Minimum Then
timer.Start()
counter += 1
If counter > 3 Then
counter = 0
End If
ElseIf Value = Maximum Then
counter = 0
timer.Stop()
End If
End Sub
Private Sub PacMan(g As Graphics, count As Integer, rect As Rectangle, valeur As Integer, colors As Color)
Select Case count
Case 0
g.FillPie(New SolidBrush(colors), CInt(rect.X + 1) + valeur, rect.Y + 1, 30, rect.Height - 4, 45, 270)
g.FillEllipse(Brushes.Black, CInt(rect.X + 12) + valeur, CInt(rect.Y + 3), 7, 7)
Case 1
g.FillPie(New SolidBrush(colors), CInt(rect.X + 1) + valeur, rect.Y + 1, 30, rect.Height - 4, 30, 300)
g.FillEllipse(Brushes.Black, CInt(rect.X + 12) + valeur, CInt(rect.Y + 3), 7, 7)
Case 2
g.FillPie(New SolidBrush(colors), CInt(rect.X + 1) + valeur, rect.Y + 1, 30, rect.Height - 4, 20, 330)
g.FillEllipse(Brushes.Black, CInt(rect.X + 12) + valeur, CInt(rect.Y + 3), 7, 7)
Case 3
g.FillPie(New SolidBrush(colors), CInt(rect.X + 1) + valeur, rect.Y + 1, 30, rect.Height - 4, 0, 360)
g.FillEllipse(Brushes.Black, CInt(rect.X + 12) + valeur, CInt(rect.Y + 3), 7, 7)
End Select
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If rect.Contains(e.Location) OrElse e.Button = Windows.Forms.MouseButtons.Left Then
moving = True
End If
End Sub
Protected Overrides Sub OnMouseup(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
moving = False
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
If moving AndAlso e.X > -1 AndAlso e.X < CInt(rect.Width + 2) Then
Value = CInt((Maximum - Minimum) * (e.X / rect.Width))
End If
MyBase.OnMouseMove(e)
End Sub
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
MyBase.OnMouseEnter(e)
If Cursor.Equals(Cursors.Arrow) Then
Me.Cursor = Cursors.Hand
End If
End Sub
Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
MyBase.OnMouseLeave(e)
If Cursor.Equals(Cursors.Hand) Then
Me.Cursor = Cursors.Arrow
End If
End Sub
Private Sub GETCirleColor(g As Graphics, colors1 As Color, colors2 As Color, rect As Rectangle, Valeur As Integer)
For i = 0 To 22 Step 2
g.FillEllipse(New SolidBrush(colors2), CInt(rect.X + 30) + i * 10, CInt(rect.Y + 10), 10, 10)
Next
If Valeur = 0 Then Return
FillRectangle(g, colors1, CInt(rect.X + 26), CInt(rect.Y + 4), Valeur, CInt(rect.Height - 8), 6)
End Sub
Protected Overrides Sub OnResize(ByVal e As EventArgs)
MyBase.OnResize(e)
If Me.Height <> 30 Then
Me.Height = 30
ElseIf Me.Width <> 300 Then
Me.Width = 300
End If
End Sub
End Class