-
Notifications
You must be signed in to change notification settings - Fork 37
/
DoubleTrackBar - No designer.vb
424 lines (357 loc) · 14.3 KB
/
DoubleTrackBar - No designer.vb
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
Imports System.ComponentModel
Public Class DoubleTrackBar
Inherits Control
Public Sub New()
Me.DoubleBuffered = True
Me.SetDefaults()
End Sub
Private Sub SetDefaults()
Me.Orientation = Windows.Forms.Orientation.Horizontal
Me.SmallChange = 1
Me.Maximum = 10
Me.Minimum = 0
Me.ValueLeft = 0
Me.ValueRight = 7
End Sub
#Region " Private Fields "
Private leftThumbState As VisualStyles.TrackBarThumbState
Private rightThumbState As VisualStyles.TrackBarThumbState
Private draggingLeft, draggingRight As Boolean
#End Region
#Region " Enums "
Public Enum Thumbs
None = 0
Left = 1
Right = 2
End Enum
#End Region
#Region " Properties "
Private _SelectedThumb As Thumbs
''' <summary>
''' Gets the thumb that had focus last.
''' </summary>
''' <returns>The thumb that had focus last.</returns>
<Description("The thumb that had focus last.")> _
Public Property SelectedThumb() As Thumbs
Get
Return _SelectedThumb
End Get
Private Set(value As Thumbs)
_SelectedThumb = value
End Set
End Property
Private _ValueLeft As Integer
''' <summary>
''' Gets or sets the position of the left slider.
''' </summary>
''' <returns>The position of the left slider.</returns>
<Description("The position of the left slider.")> _
Public Property ValueLeft() As Integer
Get
Return _ValueLeft
End Get
Set(ByVal value As Integer)
If value < Me.Minimum OrElse value > Me.Maximum Then
Throw New ArgumentException(String.Format("Value of '{0}' is not valid for 'ValueLeft'. 'ValueLeft' should be between 'Minimum' and 'Maximum'.", value.ToString()), "ValueLeft")
End If
If value > Me.ValueRight Then
Throw New ArgumentException(String.Format("Value of '{0}' is not valid for 'ValueLeft'. 'ValueLeft' should be less than or equal to 'ValueRight'.", value.ToString()), "ValueLeft")
End If
_ValueLeft = value
Me.OnValueChanged(EventArgs.Empty)
Me.OnLeftValueChanged(EventArgs.Empty)
Me.Invalidate()
End Set
End Property
Private _ValueRight As Integer
''' <summary>
''' Gets or sets the position of the right slider.
''' </summary>
''' <returns>The position of the right slider.</returns>
<Description("The position of the right slider.")> _
Public Property ValueRight() As Integer
Get
Return _ValueRight
End Get
Set(ByVal value As Integer)
If value < Me.Minimum OrElse value > Me.Maximum Then
Throw New ArgumentException(String.Format("Value of '{0}' is not valid for 'ValueRight'. 'ValueRight' should be between 'Minimum' and 'Maximum'.", value.ToString()), "ValueRight")
End If
If value < Me.ValueLeft Then
Throw New ArgumentException(String.Format("Value of '{0}' is not valid for 'ValueRight'. 'ValueRight' should be greater than or equal to 'ValueLeft'.", value.ToString()), "ValueLeft")
End If
_ValueRight = value
Me.OnValueChanged(EventArgs.Empty)
Me.OnRightValueChanged(EventArgs.Empty)
Me.Invalidate()
End Set
End Property
Private _Minimum As Integer
''' <summary>
''' Gets or sets the minimum value.
''' </summary>
''' <returns>The minimum value.</returns>
<Description("The minimum value.")> _
Public Property Minimum() As Integer
Get
Return _Minimum
End Get
Set(ByVal value As Integer)
If value >= Me.Maximum Then
Throw New ArgumentException(String.Format("Value of '{0}' is not valid for 'Minimum'. 'Minimum' should be less than 'Maximum'.", value.ToString()), "Minimum")
End If
_Minimum = value
Me.Invalidate()
End Set
End Property
Private _Maximum As Integer
''' <summary>
''' Gets or sets the maximum value.
''' </summary>
''' <returns>The maximum value.</returns>
<Description("The maximum value.")> _
Public Property Maximum() As Integer
Get
Return _Maximum
End Get
Set(ByVal value As Integer)
If value <= Me.Minimum Then
Throw New ArgumentException(String.Format("Value of '{0}' is not valid for 'Maximum'. 'Maximum' should be greater than 'Minimum'.", value.ToString()), "Maximum")
End If
_Maximum = value
Me.Invalidate()
End Set
End Property
Private _Orientation As Orientation
''' <summary>
''' Gets or sets the orientation of the control.
''' </summary>
''' <returns>The orientation of the control.</returns>
<Description("The orientation of the control.")> _
Public Property Orientation() As Orientation
Get
Return _Orientation
End Get
Set(ByVal value As Orientation)
_Orientation = value
End Set
End Property
Private _SmallChange As Integer
''' <summary>
''' Gets or sets the amount of positions the closest slider moves when the control is clicked.
''' </summary>
''' <returns>The amount of positions the closest slider moves when the control is clicked.</returns>
<Description("The amount of positions the closest slider moves when the control is clicked.")> _
Public Property SmallChange() As Integer
Get
Return _SmallChange
End Get
Set(ByVal value As Integer)
_SmallChange = value
End Set
End Property
Private ReadOnly Property RelativeValueLeft As Double
Get
Dim diff = Me.Maximum - Me.Minimum
Return If(diff = 0, Me.ValueLeft, Me.ValueLeft / diff)
End Get
End Property
Private ReadOnly Property RelativeValueRight As Double
Get
Dim diff = Me.Maximum - Me.Minimum
Return If(diff = 0, Me.ValueLeft, Me.ValueRight / diff)
End Get
End Property
#End Region
#Region " Methods "
Public Sub IncrementLeft()
Dim newValue = Math.Min(Me.ValueLeft + 1, Me.Maximum)
If Me.IsValidValueLeft(newValue) Then
Me.ValueLeft = newValue
End If
Me.Invalidate()
End Sub
Public Sub IncrementRight()
Dim newValue = Math.Min(Me.ValueRight + 1, Me.Maximum)
If Me.IsValidValueRight(newValue) Then
Me.ValueRight = newValue
End If
Me.Invalidate()
End Sub
Public Sub DecrementLeft()
Dim newValue = Math.Max(Me.ValueLeft - 1, Me.Minimum)
If Me.IsValidValueLeft(newValue) Then
Me.ValueLeft = newValue
End If
Me.Invalidate()
End Sub
Public Sub DecrementRight()
Dim newValue = Math.Max(Me.ValueRight - 1, Me.Minimum)
If Me.IsValidValueRight(newValue) Then
Me.ValueRight = newValue
End If
Me.Invalidate()
End Sub
Private Function IsValidValueLeft(ByVal value As Integer) As Boolean
Return (value >= Me.Minimum AndAlso value <= Me.Maximum AndAlso value < Me.ValueRight)
End Function
Private Function IsValidValueRight(ByVal value As Integer) As Boolean
Return (value >= Me.Minimum AndAlso value <= Me.Maximum AndAlso value > Me.ValueLeft)
End Function
Private Function GetLeftThumbRectangle(Optional ByVal g As Graphics = Nothing) As Rectangle
Dim shouldDispose = (g Is Nothing)
If shouldDispose Then g = Me.CreateGraphics()
Dim rect = Me.GetThumbRectangle(Me.RelativeValueLeft, g)
If shouldDispose Then g.Dispose()
Return rect
End Function
Private Function GetRightThumbRectangle(Optional ByVal g As Graphics = Nothing) As Rectangle
Dim shouldDispose = (g Is Nothing)
If shouldDispose Then g = Me.CreateGraphics()
Dim rect = Me.GetThumbRectangle(Me.RelativeValueRight, g)
If shouldDispose Then g.Dispose()
Return rect
End Function
Private Function GetThumbRectangle(ByVal relativeValue As Double, ByVal g As Graphics) As Rectangle
Dim size = TrackBarRenderer.GetBottomPointingThumbSize(g, VisualStyles.TrackBarThumbState.Normal)
Dim border = CInt(size.Width / 2)
Dim w = Me.GetTrackRectangle(border).Width
Dim x = CInt(Math.Abs(Me.Minimum) / (Me.Maximum - Me.Minimum) * w + relativeValue * w)
Dim y = CInt((Me.Height - size.Height) / 2)
Return New Rectangle(New Point(x, y), size)
End Function
Private Function GetTrackRectangle(ByVal border As Integer) As Rectangle
Return New Rectangle(border, CInt(Me.Height / 2) - 3, Me.Width - 2 * border - 1, 4)
End Function
Private Function GetClosestSlider(ByVal point As Point) As Thumbs
Dim leftThumbRect = Me.GetLeftThumbRectangle()
Dim rightThumbRect = Me.GetRightThumbRectangle()
If Me.Orientation = Windows.Forms.Orientation.Horizontal Then
If Math.Abs(leftThumbRect.X - point.X) > Math.Abs(rightThumbRect.X - point.X) _
AndAlso Math.Abs(leftThumbRect.Right - point.X) > Math.Abs(rightThumbRect.Right - point.X) Then
Return Thumbs.Right
Else
Return Thumbs.Left
End If
Else
If Math.Abs(leftThumbRect.Y - point.Y) > Math.Abs(rightThumbRect.Y - point.Y) _
AndAlso Math.Abs(leftThumbRect.Bottom - point.Y) > Math.Abs(rightThumbRect.Bottom - point.Y) Then
Return Thumbs.Right
Else
Return Thumbs.Left
End If
End If
End Function
Private Sub SetThumbState(ByVal location As Point, ByVal newState As VisualStyles.TrackBarThumbState)
Dim leftThumbRect = Me.GetLeftThumbRectangle()
Dim rightThumbRect = Me.GetRightThumbRectangle()
If leftThumbRect.Contains(location) Then
leftThumbState = newState
Else
If Me.SelectedThumb = Thumbs.Left Then
leftThumbState = VisualStyles.TrackBarThumbState.Hot
Else
leftThumbState = VisualStyles.TrackBarThumbState.Normal
End If
End If
If rightThumbRect.Contains(location) Then
rightThumbState = newState
Else
If Me.SelectedThumb = Thumbs.Right Then
rightThumbState = VisualStyles.TrackBarThumbState.Hot
Else
rightThumbState = VisualStyles.TrackBarThumbState.Normal
End If
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
Me.SetThumbState(e.Location, VisualStyles.TrackBarThumbState.Hot)
Dim offset = CInt(e.Location.X / (Me.Width) * (Me.Maximum - Me.Minimum))
Dim newValue = Me.Minimum + offset
If draggingLeft Then
If Me.IsValidValueLeft(newValue) Then Me.ValueLeft = newValue
ElseIf draggingRight Then
If Me.IsValidValueRight(newValue) Then Me.ValueRight = newValue
End If
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
Me.Focus()
Me.SetThumbState(e.Location, VisualStyles.TrackBarThumbState.Pressed)
draggingLeft = (leftThumbState = VisualStyles.TrackBarThumbState.Pressed)
If Not draggingLeft Then draggingRight = (rightThumbState = VisualStyles.TrackBarThumbState.Pressed)
If draggingLeft Then
Me.SelectedThumb = Thumbs.Left
ElseIf draggingRight Then
Me.SelectedThumb = Thumbs.Right
End If
If Not draggingLeft AndAlso Not draggingRight Then
If Me.GetClosestSlider(e.Location) = 1 Then
If e.X < Me.GetLeftThumbRectangle().X Then
Me.DecrementLeft()
Else
Me.IncrementLeft()
End If
Me.SelectedThumb = Thumbs.Left
Else
If e.X < Me.GetRightThumbRectangle().X Then
Me.DecrementRight()
Else
Me.IncrementRight()
End If
Me.SelectedThumb = Thumbs.Right
End If
End If
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
draggingLeft = False
draggingRight = False
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseWheel(e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseWheel(e)
If e.Delta = 0 Then Return
If Me.SelectedThumb = 1 Then
If e.Delta > 0 Then
Me.IncrementLeft()
Else
Me.DecrementLeft()
End If
ElseIf Me.SelectedThumb = 2 Then
If e.Delta > 0 Then
Me.IncrementRight()
Else
Me.DecrementRight()
End If
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim thumbSize = Me.GetThumbRectangle(0, e.Graphics).Size
Dim trackRect = Me.GetTrackRectangle(CInt(thumbSize.Width / 2))
Dim ticksRect = trackRect : ticksRect.Offset(0, 15)
TrackBarRenderer.DrawVerticalTrack(e.Graphics, trackRect)
TrackBarRenderer.DrawHorizontalTicks(e.Graphics, ticksRect, Me.Maximum - Me.Minimum + 1, VisualStyles.EdgeStyle.Etched)
TrackBarRenderer.DrawBottomPointingThumb(e.Graphics, Me.GetLeftThumbRectangle(e.Graphics), leftThumbState)
TrackBarRenderer.DrawBottomPointingThumb(e.Graphics, Me.GetRightThumbRectangle(e.Graphics), rightThumbState)
End Sub
#End Region
#Region " Events "
Public Event ValueChanged As EventHandler
Public Event LeftValueChanged As EventHandler
Public Event RightValueChanged As EventHandler
Protected Overridable Sub OnValueChanged(e As EventArgs)
RaiseEvent ValueChanged(Me, e)
End Sub
Protected Overridable Sub OnLeftValueChanged(e As EventArgs)
RaiseEvent LeftValueChanged(Me, e)
End Sub
Protected Overridable Sub OnRightValueChanged(e As EventArgs)
RaiseEvent RightValueChanged(Me, e)
End Sub
#End Region
End Class