1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Public Class RotationRectangle
Inherits Control
Private min As Integer = 0 ' Minimum value for progress range
Private max As Integer = 100 ' Maximum value for progress range
Private val As Integer = 0 ' Current progress
Private Horz As Boolean = True
Private m_barColor As Color = Color.Blue
<Browsable(True)>
Public Property BarColor() As Color
Get
Return m_barColor
End Get
Set(ByVal Value As Color)
If Value <> m_barColor Then
m_barColor = Value
Me.Invalidate()
End If
End Set
End Property
<Browsable(True)>
Public Property Horizontal() As Boolean
Get
Return Horz
End Get
Set(ByVal Value As Boolean)
If Value <> Horz Then
Dim tmp As Integer = Me.Width
Me.Width = Me.Height
Me.Height = tmp
End If
Horz = Value
End Set
End Property
<Browsable(True)>
Public Property Maximum As Integer
Get
Return max
End Get
Set(ByVal value As Integer)
' Make sure that the maximum value is never set lower than the minimum value.
If (value < min) Then
min = value
End If
max = value
' Make sure that the value is still in range.
If (val > max) Then
val = max
End If
Invalidate()
End Set
End Property
<Browsable(True)>
Public Property Minimum As Integer
Get
Return min
End Get
Set(ByVal value As Integer)
' Prevent a negative value.
If (value < 0) Then
min = 0
End If
' Make sure that the minimum value is never set higher than the maximum value.
If (value > max) Then
min = value
End If
' Make sure that the value is still in range.
If (val < min) Then
val = min
End If
Invalidate()
End Set
End Property
<Browsable(True)>
Public Property Value As Integer
Get
Return val
End Get
Set(ByVal value As Integer)
' Make sure that the value does not stray outside the valid range.
If (value < min) Then
val = min
ElseIf (value > max) Then
val = max
Else
val = value
End If
Invalidate()
End Set
End Property
Public Sub New()
InitializeComponent()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or _
ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)
Me.Size = New Size(200, 20)
Me.DoubleBuffered = True
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim percent As Decimal = (Value - min) / (max - min)
Dim rect As New Rectangle(0, 0, Me.ClientRectangle.Width - 1, Me.ClientRectangle.Height - 1)
Dim gr As Graphics = e.Graphics
Dim brush As SolidBrush = New SolidBrush(m_barColor)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
With Me.ClientRectangle
If Horz Then
Dim bar As New Rectangle(0, 0, rect.Width, rect.Height) 'Horizental
' Calculate area for drawing the progress.
bar.Width = rect.Width * percent
gr.FillRectangle(brush, bar)
Else
Dim bar As New Rectangle(0, 0, rect.Height, rect.Width) ' vertical
' Calculate area for drawing the progress.
bar.Height = rect.Height * percent
gr.FillRectangle(brush, bar)
End If
End With
gr.DrawRectangle(New Pen(New SolidBrush(Color.Red)), rect)
End Sub
Protected Overrides Sub OnResize(ByVal e As EventArgs)
MyBase.OnResize(e)
Me.Invalidate()
End Sub
End Class |
Partager