Public Class CustomProgressBar
Inherits System.Windows.Forms.ProgressBar
Enum BarTextStyle
DefaultText = 0
ValueText = 1
ValueOfMax = 2
PercentText = 3
End Enum
Enum BrushBarStyle
Block = 0
GradientHorizontal = 1
GradientVertical = 2
Solid = 3
Picture = 4
End Enum
Private StartFontSize As Single
Private _BarStyle As BrushBarStyle
Private _BackColor As System.Drawing.Color
Private _Picture As System.Drawing.Image
Private _TextStyle As BarTextStyle
Private _Font As System.Drawing.Font
Private _FontColor As System.Drawing.Color
Private _AllowFontHighlight As Boolean
Private _FontAutoAdjust As Boolean
Sub New()
MyBase.New()
Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(Windows.Forms.ControlStyles.UserPaint, True)
Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)
Me.BarStyle = BrushBarStyle.Solid
Me.ControlColor = System.Drawing.SystemColors.Control
Me.BackColor = System.Drawing.SystemColors.Control
Me.ForeColor = System.Drawing.SystemColors.Highlight
Me.Font = MyBase.Font
Me.FontColor = System.Drawing.SystemColors.WindowText
Me.TextStyle = BarTextStyle.DefaultText
Me.StartFontSize = Me.Font.Size
Me.AllowFontHighlight = True
Me.FontAutoSize = True
End Sub
#Region "Properties"
''' <summary>
''' Gets or sets the current progress.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the current progress.")> _
Public Shadows Property Value() As Integer
Get
Return MyBase.Value
End Get
Set(ByVal value As Integer)
If value < Me.Minimum Then
value = Me.Minimum
ElseIf value > Me.Maximum Then
value = Me.Maximum
End If
MyBase.Value = value
RaiseEvent ValueChanged(Me)
Me.Invalidate()
End Set
End Property
''' <summary>
''' Gets or sets the style of the progress bar.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the style of the progress bar."), ComponentModel.DefaultValue(BrushBarStyle.Solid)> _
Public Property BarStyle() As BrushBarStyle
Get
Return _BarStyle
End Get
Set(ByVal value As BrushBarStyle)
_BarStyle = value
End Set
End Property
''' <summary>
''' Gets or sets the progress bar color.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the progress bar color"), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Highlight")> _
Public Overrides Property ForeColor() As System.Drawing.Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal value As System.Drawing.Color)
MyBase.ForeColor = value
End Set
End Property
''' <summary>
''' Gets or sets the back color of the progress bar. This is used with Gradient style progress bars.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the back color of the progress bar. This is used with Gradient style progress bars."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Control"), ComponentModel.Browsable(True)> _
Public Overrides Property BackColor() As System.Drawing.Color
Get
Return _BackColor
End Get
Set(ByVal value As System.Drawing.Color)
_BackColor = value
End Set
End Property
''' <summary>
''' Gets or sets the color of the control.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the the color of the control."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Control")> _
Public Property ControlColor() As System.Drawing.Color
Get
Return MyBase.BackColor
End Get
Set(ByVal value As System.Drawing.Color)
MyBase.BackColor = value
End Set
End Property
''' <summary>
''' Gets or sets a picture that will be used instead of a color to show progress.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets a picture that will be used instead of a color to show progress.")> _
Public Property Picture() As System.Drawing.Image
Get
Return _Picture
End Get
Set(ByVal value As System.Drawing.Image)
_Picture = value
End Set
End Property
''' <summary>
''' Gets or sets the text to be displayed when the TextStyle is set to DefaultText.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the text to be displayed when the TextStyle is set to DefaultText."), System.ComponentModel.Browsable(True)> _
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
''' <summary>
''' Gets or sets the TextStyle used when outputting text.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the TextStyle used when outputting text."), ComponentModel.DefaultValue(BarTextStyle.DefaultText)> _
Public Property TextStyle() As BarTextStyle
Get
Return _TextStyle
End Get
Set(ByVal value As BarTextStyle)
_TextStyle = value
End Set
End Property
''' <summary>
''' Gets or sets the properties of the font used.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Browsable(True)> _
Public Overrides Property Font() As System.Drawing.Font
Get
Return _Font
End Get
Set(ByVal value As System.Drawing.Font)
_Font = value
End Set
End Property
''' <summary>
''' Gets or sets the font color for the text displayed.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets the font color for the text displayed."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "WindowText")> _
Public Property FontColor() As System.Drawing.Color
Get
Return _FontColor
End Get
Set(ByVal value As System.Drawing.Color)
_FontColor = value
End Set
End Property
''' <summary>
''' Gets or sets if the text should highlight as the progress bar crosses the text.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets if the text should highlight as the progress bar crosses the text."), ComponentModel.DefaultValue(True)> _
Public Property AllowFontHighlight() As Boolean
Get
Return _AllowFontHighlight
End Get
Set(ByVal value As Boolean)
_AllowFontHighlight = value
End Set
End Property
''' <summary>
''' Gets or sets if the size of the font should adjust depending on the size of the control.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.Description("Sets if the size of the font should adjust depending on the size of the control."), ComponentModel.DefaultValue(True)> _
Public Property FontAutoSize() As Boolean
Get
Return _FontAutoAdjust
End Get
Set(ByVal value As Boolean)
_FontAutoAdjust = value
End Set
End Property
#End Region
#Region "Events"
Public Event ValueChanged(ByVal sender As Object)
'Public Sub cpm_ValueChanged(ByVal sender As Object)
' If Me IsNot Nothing Then
' Me.CreateGraphics.Clear(Me.ControlColor)
' PaintBar(Me.CreateGraphics)
' PaintText(Me.CreateGraphics)
' 'Me.Invalidate()
' End If
' RaiseEvent ValueChanged(sender)
'End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.Clear(Me.ControlColor)
If Me IsNot Nothing Then
PaintBar(e.Graphics)
PaintText(e.Graphics)
End If
MyBase.OnPaint(e)
End Sub
'Paint a bar graphic to the control
Private Sub PaintBar(ByVal sender As System.Drawing.Graphics)
'If text is painted then a bar must be painted as it will no longer show on its own.
Dim percentage As Double = (Me.Value / Me.Maximum * Me.ClientRectangle.Width)
Dim rect As New System.Drawing.RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, percentage, Me.ClientRectangle.Height)
Dim BarBrush As System.Drawing.Brush
Select Case _BarStyle
Case BrushBarStyle.Block
BarBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing.Drawing2D.HatchStyle.Vertical, Me.ControlColor, Me.ForeColor)
Case BrushBarStyle.GradientHorizontal
If percentage > 0 Then BarBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me.ForeColor, Me.BackColor, Drawing.Drawing2D.LinearGradientMode.Horizontal)
Case BrushBarStyle.GradientVertical
If percentage > 0 Then BarBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me.ForeColor, Me.BackColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
Case BrushBarStyle.Solid
BarBrush = New System.Drawing.SolidBrush(Me.ForeColor)
Case BrushBarStyle.Picture
If _Picture IsNot Nothing Then BarBrush = New System.Drawing.TextureBrush(_Picture, Drawing.Drawing2D.WrapMode.Tile)
End Select
If BarBrush Is Nothing Then BarBrush = New System.Drawing.SolidBrush(Me.ForeColor)
sender.SmoothingMode = Drawing.Drawing2D.SmoothingMode.Default
sender.FillRectangle(BarBrush, rect)
BarBrush.Dispose()
End Sub
'Paint text to the control
Private Sub PaintText(ByVal sender As System.Drawing.Graphics)
Dim x As Decimal 'Double 'Integer 'Single
Dim y As Decimal 'Double 'Integer 'Single
Dim text As String = " "
Dim sizeF As System.Drawing.SizeF
Select Case Me.TextStyle
Case BarTextStyle.DefaultText
text = Me.Text
Case BarTextStyle.ValueText
text = Me.Value
Case BarTextStyle.ValueOfMax
text = Me.Value & "/" & Me.Maximum & " " & Me.Font.Size & "/" & Me.Font.Size
Case BarTextStyle.PercentText
text = (Me.Value / Me.Maximum * 100)
End Select
'sizeF = Me.CreateGraphics.MeasureString(text, Me.Font, Me.Width)
sizeF = Me.CreateGraphics.MeasureString(text, Me.Font, Len(text) * Me.Font.SizeInPoints)
If Me.FontAutoSize = True Then
If sizeF.ToSize.Height < Me.ClientRectangle.Height - 5 Then sizeF = FontAdjust(sizeF)
End If
x = (Me.ClientRectangle.Width / 2) - (sizeF.ToSize.Width / 2)
y = (Me.ClientRectangle.Height / 2) - (sizeF.ToSize.Height / 2)
'Dim TextFormat As New System.Drawing.StringFormat
'TextFormat.Alignment = Drawing.StringAlignment.Center
'TextFormat.LineAlignment = Drawing.StringAlignment.Center
Dim percentage As Double = (Me.Value / Me.Maximum * Me.ClientRectangle.Width)
''Dim TempColor As System.Drawing.Color
''If percentage > (x + sizeF.ToSize.Width) Then
'' TempColor = System.Drawing.SystemColors.HighlightText
''Else
'' TempColor = Me.FontColor
''End If
'sender.DrawString(text, Me.Font, New System.Drawing.SolidBrush(Me.FontColor), x, y)
sender.DrawString(text, Me.Font, New System.Drawing.SolidBrush(Me.FontColor), New System.Drawing.RectangleF(x, y, sizeF.Width, sizeF.Height))
'If percentage > x And percentage < (x + sizeF.ToSize.Width) Then
'Dim InText As Integer = Math.Ceiling((percentage - x) / text.Length)
'Dim Text1 As String = text.Substring(0, InText)
If Me.AllowFontHighlight = True Then
sender.DrawString(text, Me.Font, New System.Drawing.SolidBrush(System.Drawing.SystemColors.HighlightText), New System.Drawing.RectangleF(x, y, (percentage - x) + 2, sizeF.Height))
End If
'sender.DrawString(text, Me.Font, New System.Drawing.SolidBrush(System.Drawing.SystemColors.HighlightText), New System.Drawing.RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, (percentage - x), Me.ClientRectangle.Height))
'End If
End Sub
Private Function FontAdjust(ByVal Size As System.Drawing.SizeF) As System.Drawing.SizeF
Dim sizeF As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(Text, Me.Font, Me.ClientRectangle.Width)
If sizeF.ToSize.Height < Me.ClientRectangle.Height - 5 Or sizeF.ToSize.Height > Me.ClientRectangle.Height Then
If Me.ClientRectangle.Height > 2 Then
sizeF = New System.Drawing.SizeF(sizeF.Width, Me.ClientRectangle.Height)
Me.Font = New System.Drawing.Font(Me.Font.FontFamily, Me.ClientRectangle.Height, Me.Font.Style, Drawing.GraphicsUnit.Pixel)
Return sizeF
End If
End If
Return sizeF
End Function
End Class