'--- CustomProgressBar.vb --- Imports System.ComponentModel <DefaultEvent("ValueChanged"), DefaultProperty("Value")> _ Public Class CustomProgressBar Inherits Control Private _Maximum As Integer = 100 Private _Minimum As Integer = 0 Private _Value As Integer = 0 Public Event ValueChanged As EventHandler Public Sub New() MyBase.AutoSize = False MyBase.Text = "0%" End Sub <Browsable(False)> _ <EditorBrowsable(EditorBrowsableState.Never)> _ Public Overrides Property Text() As String Get Return Nothing End Get Set(ByVal value As String) End Set End Property <DefaultValue(100)> _ Public Property Maximum() As Integer Get Return _Maximum End Get Set(ByVal value As Integer) _Maximum = value If Me.Value > value Then Me.Value = value Else Me.Value = Me.Value End If End Set End Property <DefaultValue(0)> _ Public Property Minimum() As Integer Get Return _Minimum End Get Set(ByVal value As Integer) _Minimum = value If Me.Value < value Then Me.Value = value Else Me.Value = Me.Value End If End Set End Property <DefaultValue(0)> _ Public Property Value() As Integer Get Return _Value End Get Set(ByVal newValue As Integer) If newValue < Minimum OrElse Maximum < newValue Then Throw New ArgumentOutOfRangeException("Value", newValue, _ "'Value' は 'Minimum' と 'Maximum' の間でなければなりません。") End If _Value = newValue Invalidate() RaiseEvent ValueChanged(Me, EventArgs.Empty) End Set End Property Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) MyBase.OnPaint(e) Dim chunksRect As Rectangle = ClientRectangle chunksRect.Inflate(-3, -3) If (Maximum - Minimum) = 0 Then chunksRect.Width = 0 Else Dim dbl As Double = (Value - Minimum) / (Maximum - Minimum) chunksRect.Width = CInt(chunksRect.Width * dbl) End If Dim Caption As String = "0%" If Value <> Minimum Then Dim dec As Decimal = CDec(Value - Minimum) / CDec(Maximum - Minimum) Caption = FormatPercent(dec, 0) End If ProgressBarRenderer.DrawHorizontalBar(e.Graphics, ClientRectangle) ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, chunksRect) TextRenderer.DrawText(e.Graphics, Caption, Me.Font, ClientRectangle, Me.ForeColor) End Sub End Class