-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSlider.cs
347 lines (288 loc) · 11.1 KB
/
Slider.cs
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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Region_Editor
{
public partial class Slider : UserControl
{
private int _minValue = 0;
private int _maxValue = 100;
private int _smallChange = 1;
private int _value = 0;
private bool _dragModeEnabled = false;
private Point _startDragPoint;
private Point _clickPoint;
[Bindable(true), Category("Behavior"), DefaultValue(0), Description("The minimum value of the slider")]
public int Minimum
{
get { return _minValue; }
set
{
_minValue = value;
if (_value < _minValue)
Value = _minValue;
else
{
MoveSlider();
Invalidate();
}
}
}
[Bindable(true), Category("Behavior"), DefaultValue(0), Description("The value of the slider")]
public int Value
{
get { return _value; }
set
{
if (value < _minValue || value > _maxValue)
return;
if (value == _value)
return;
int tmp = _value;
_value = value;
MoveSlider();
}
}
[Bindable(true), Category("Behavior"), DefaultValue(100), Description("The maximum value of the slider")]
public int Maximum
{
get { return _maxValue; }
set
{
_maxValue = value;
if (_value > _maxValue)
Value = _maxValue;
else
{
MoveSlider();
Invalidate();
}
}
}
[Bindable(true), Category("Appearance"), DefaultValue(1), Description("The number of positions the slider moves by clicking")]
public int SmallChange
{
get { return _smallChange; }
set
{
_smallChange = value;
}
}
private Orientation _Orientation = Orientation.Horizontal;
[Bindable(true), Category("Behavior"), DefaultValue(typeof(Orientation), "Horizontal"), Description("The orientation of the slider control.")]
public Orientation Orientation
{
get { return _Orientation; }
set
{
if (_Orientation != value)
{
_Orientation = value;
Size size;
switch (_Orientation)
{
case System.Windows.Forms.Orientation.Horizontal:
size = new Size(Height, Width);
MinimumSize = new Size(100, 22);
MaximumSize = new Size(1000, 22);
Size = size;
break;
case System.Windows.Forms.Orientation.Vertical:
size = new Size(Height, Width);
MinimumSize = new Size(22, 100);
MaximumSize = new Size(22, 1000);
Size = size;
break;
}
OnResize(new EventArgs());
}
}
}
[Category("Action"), Description("Occurs when the slider is moved")]
public event EventHandler ValueChanged;
[Category("Action"), Description("Occurs when the user is done moving the slider")]
public event EventHandler SliderMoveComplete;
public Slider()
{
InitializeComponent();
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.Selectable, false);
SetStyle(ControlStyles.SupportsTransparentBackColor, false);
SetStyle(ControlStyles.UserMouse, true);
SetStyle(ControlStyles.UserPaint, true);
TabStop = false;
slideControl.Left = 0;
slideControl.Invalidate();
MoveSlider();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (_Orientation == System.Windows.Forms.Orientation.Horizontal)
{
slideControl.Size = new System.Drawing.Size(10, 20);
slideControl.Left = 0;
slideControl.Top = 1;
}
else
{
slideControl.Size = new System.Drawing.Size(20, 10);
slideControl.Left = 1;
slideControl.Top = 0;
}
slideControl.Invalidate();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(BackColor);
if (_Orientation == Orientation.Horizontal)
{
e.Graphics.DrawLine(Pens.DarkGray, 0, (Height / 2), Width, (Height / 2));
e.Graphics.DrawLine(Pens.LightGray, 0, (Height / 2) + 1, Width, (Height / 2) + 1);
}
else
{
e.Graphics.DrawLine(Pens.DarkGray, (Width / 2), 0, (Width / 2), Height);
e.Graphics.DrawLine(Pens.LightGray, (Width / 2) + 1, 0, (Width / 2) + 1, Height);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
_clickPoint = e.Location;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
int newValue = _value;
if (_Orientation == System.Windows.Forms.Orientation.Horizontal)
{
if (_clickPoint.X < slideControl.Left)
newValue -= _smallChange;
else
newValue += _smallChange;
if (newValue < _minValue)
newValue = _minValue;
else if (newValue > _maxValue)
newValue = _maxValue;
}
else
{
if (_clickPoint.Y < slideControl.Top)
newValue -= _smallChange;
else
newValue += _smallChange;
if (newValue < _minValue)
newValue = _minValue;
else if (newValue > _maxValue)
newValue = _maxValue;
}
Value = newValue;
if (SliderMoveComplete != null)
SliderMoveComplete(this, new EventArgs());
}
private void MoveSlider(int delta)
{
if (_Orientation == System.Windows.Forms.Orientation.Horizontal)
{
if (delta < 0 && (slideControl.Left + delta) <= 0)
slideControl.Left = 0;
else if (delta > 0 && (slideControl.Left + slideControl.Width + delta) >= Width)
slideControl.Left = Width - slideControl.Width;
else
slideControl.Left += delta;
}
else
{
if (delta < 0 && (slideControl.Top + delta) <= 0)
slideControl.Top = 0;
else if (delta > 0 && (slideControl.Top + slideControl.Height + delta) >= Height)
slideControl.Top = Height - slideControl.Height;
else
slideControl.Top += delta;
}
CalculateSliderValue();
}
private void MoveSlider()
{
if (_Orientation == System.Windows.Forms.Orientation.Horizontal)
{
float valueLength = (float)(Width - slideControl.Width) / (float)(_maxValue - _minValue);
slideControl.Left = (int)(valueLength * (float)_value);
}
else
{
float valueLength = (float)(Height - slideControl.Height) / (float)(_maxValue - _minValue);
slideControl.Top = (int)(valueLength * (float)_value);
}
}
private void CalculateSliderValue()
{
if (_Orientation == System.Windows.Forms.Orientation.Horizontal)
{
float valueLength = (float)(_maxValue - _minValue) / (float)(Width - slideControl.Width);
_value = (int)((float)slideControl.Left * valueLength);
}
else
{
float valueLength = (float)(_maxValue - _minValue) / (float)(Height - slideControl.Height);
_value = (int)((float)slideControl.Top * valueLength);
}
if (ValueChanged != null)
ValueChanged(this, new EventArgs());
}
private void slideControl_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
int w = slideControl.Width - 1;
int h = slideControl.Height - 1;
e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control), 0, 0, w, h);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight), 0, 0, w, 0);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLightLight), 0, 0, 0, h);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLight), 1, 1, w - 1, 1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlLight), 1, 1, 1, h - 1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDarkDark), w, h, w, 1);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDarkDark), w, h, 1, h);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark), w - 1, h - 1, w - 1, 2);
e.Graphics.DrawLine(new Pen(SystemColors.ControlDark), w - 1, h - 1, 2, h - 1);
}
private void slideControl_MouseDown(object sender, MouseEventArgs e)
{
_dragModeEnabled = true;
_startDragPoint = new Point(e.X, e.Y);
}
private void slideControl_MouseMove(object sender, MouseEventArgs e)
{
if (this._dragModeEnabled == false)
return;
int delta = 0;
if (_Orientation == System.Windows.Forms.Orientation.Horizontal)
delta = e.X - this._startDragPoint.X;
else
delta = e.Y - this._startDragPoint.Y;
if (delta == 0)
return;
this.MoveSlider(delta);
}
private void slideControl_MouseUp(object sender, MouseEventArgs e)
{
_dragModeEnabled = false;
if (SliderMoveComplete != null)
SliderMoveComplete(this, new EventArgs());
}
public void InvokeValueChanged(object sender, EventArgs e)
{
if (ValueChanged != null)
ValueChanged(sender, e);
}
public void InvokeSliderMoveComplete(object sender, EventArgs e)
{
if (SliderMoveComplete != null)
SliderMoveComplete(sender, e);
}
}
}