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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| Public Class Trackbar1 : Inherits Control
Private _ThumbTwoColor As Boolean
Public Property ThumbTwoColor As Boolean
Get
Return _ThumbTwoColor
End Get
Set(value As Boolean)
_ThumbTwoColor = value
Me.Invalidate()
End Set
End Property
Private _TickFrequency As Integer
Public Property TickFrequency As Integer
Get
Return _TickFrequency
End Get
Set(value As Integer)
_TickFrequency = value
Me.Invalidate()
End Set
End Property
Private _minimum As Integer
Public Property Minimum() As Integer
Get
Return _minimum
End Get
Set(ByVal value As Integer)
_minimum = value
If value > _maximum Then _maximum = value
If value > _value Then _value = value
Invalidate()
End Set
End Property
Private _maximum As Integer
Public Property Maximum() As Integer
Get
Return _maximum
End Get
Set(ByVal value As Integer)
_maximum = value
If value < _minimum Then _minimum = value
If value < _value Then _value = value
Invalidate()
End Set
End Property
Event ValueChanged()
Private _value As Integer
Public Property Value() As Integer
Get
Return _value
End Get
Set(ByVal value As Integer)
If value < _minimum Then
_value = _minimum
ElseIf value > _maximum Then
_value = _maximum
Else
_value = value
End If
Invalidate()
RaiseEvent ValueChanged()
End Set
End Property
Private _trackbar As TypeTrackBar
Public Property MyCuseur As TypeTrackBar
Get
Return _trackbar
End Get
Set(value As TypeTrackBar)
_trackbar = value
Me.Invalidate()
End Set
End Property
Private _FillColor As Color = Color.White
Public Property FillColor As Color
Get
Return _FillColor
End Get
Set(value As Color)
If value = Color.Transparent Then
Return
Else
_FillColor = value
Me.Invalidate()
End If
End Set
End Property
Private _LineColor As Color = Color.Gray
Public Property LineColor As Color
Get
Return _LineColor
End Get
Set(value As Color)
If value = Color.Transparent Then
Return
Else
_LineColor = value
Me.Invalidate()
End If
End Set
End Property
Private _CuseurColor2 As Color = Color.White
Public Property CuseurColor2 As Color
Get
Return _CuseurColor2
End Get
Set(value As Color)
_CuseurColor2 = value
Me.Invalidate()
End Set
End Property
Private _CuseurColor As Color = Color.DarkBlue
Public Property CuseurColor As Color
Get
Return _CuseurColor
End Get
Set(value As Color)
If value = Color.Transparent Then
Return
Else
_CuseurColor = value
Me.Invalidate()
End If
End Set
End Property
Private Rect As New Rectangle
Private Moving As Boolean = False
Public Sub New()
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
Me.Size = New Size(210, 50)
Me._trackbar = TypeTrackBar.Rectangle
Me._maximum = 100
Me._minimum = 0
Me._TickFrequency = 1
Me._ThumbTwoColor = True
End Sub
Protected Overrides Sub OnResize(ByVal e As EventArgs)
MyBase.OnResize(e)
If Me.Height <> 50 Then
Me.Height = 50
ElseIf Me.Width < 200 Then
Me.Width = 200
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Rect = New Rectangle(0, 0, Me.Width, Me.Height)
e.Graphics.FillRectangle(New SolidBrush(_FillColor), New Rectangle(0, 0, Rect.Width - 1, Rect.Height - 1))
e.Graphics.DrawLine(New Pen(New SolidBrush(_LineColor), 5), New Point(5, 10), New Point(Rect.Width - 5, 10))
Select Case _trackbar
Case TypeTrackBar.Rectangle
e.Graphics.FillRectangle(New SolidBrush(_CuseurColor), New Rectangle(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0, 15, 25))
Case TypeTrackBar.Circle
e.Graphics.FillEllipse(New SolidBrush(_CuseurColor), (Rect.X - 1) + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0, 20, 20)
Case TypeTrackBar.CustomThumb
Dim size As New Size((Rect.X + 15) + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), Rect.Height - 20)
e.Graphics.FillPolygon(New SolidBrush(_CuseurColor), New Point() {New Point(size.Width - 15, 1), New Point(size.Width, 1), New Point(size.Width, size.Height - 12), New Point((size.Width - 7), size.Height - 6), New Point(size.Width - 15, size.Height - 12)})
If _ThumbTwoColor Then
e.Graphics.FillPolygon(New SolidBrush(_CuseurColor2), New Point() {New Point(size.Width - 15, 4), New Point(size.Width, 4), New Point(size.Width, size.Height - 16), New Point((size.Width - 7), size.Height - 7), New Point(size.Width - 15, size.Height - 16)})
End If
End Select
e.Graphics.DrawLine(New Pen(New SolidBrush(_CuseurColor), 5), New Point(0, 10), New Point(CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 10))
DrawLine(e.Graphics, Rect)
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
If Rect.Contains(e.Location) Then
Dim mp = New Rectangle(New Point(e.Location.X, e.Location.Y), New Size(1, 1))
If New Rectangle(New Point(Rect.X + CInt(((Rect.Width - 20) / (Maximum - Minimum)) * Value), 0), New Size(Rect.Width, Height)).IntersectsWith(mp) Then
Moving = True
End If
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 Then
Dim mp As Point = New Point(e.X, e.Y)
Value = CInt(Maximum * ((mp.X - Rect.X) / Rect.Width))
End If
End Sub
Public Enum TypeTrackBar
Rectangle = 0
Circle = 1
CustomThumb = 2
End Enum
Private Sub DrawLine(g As Graphics, Rect As Rectangle)
Dim tickFrequencySize As Double = ((Size.Width - 20) * Me.TickFrequency / (Me.Maximum - Me.Minimum))
Dim tickCount As Integer = CInt(((Me.Maximum - Me.Minimum) / Me.TickFrequency)) + 1
g.DrawLine(New Pen(Brushes.Black, 1), New Point(CInt(Rect.X + 8), Rect.Height / 2), New Point(CInt(Rect.Width - 12), Rect.Height / 2))
For i = 0 To tickCount
If i Mod 10 = 0 Then
g.DrawLine((New Pen(Brushes.Black, 1)), CInt((Rect.X + 8) + tickFrequencySize * i), CInt(Rect.Height / 2), CInt((Rect.X + 8) + tickFrequencySize * i), CInt(Rect.Height / 1.5))
g.DrawString(i.ToString, Me.Font, Brushes.Black, New Point(CInt((Rect.X + 8) + tickFrequencySize * i), Rect.Height / 1.5), New StringFormat With {.Alignment = StringAlignment.Center})
End If
Next
End Sub
End Class |
Partager