-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgressIndicator.cs
More file actions
439 lines (360 loc) · 13.1 KB
/
ProgressIndicator.cs
File metadata and controls
439 lines (360 loc) · 13.1 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;
namespace ProgressControls
{
/// <summary>
/// Firefox like circular progress indicator.
/// </summary>
public partial class ProgressIndicator : Control
{
#region Constructor
/// <summary>
/// Default constructor for the ProgressIndicator.
/// </summary>
public ProgressIndicator()
{
InitializeComponent();
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
if (AutoStart)
timerAnimation.Start();
}
#endregion
#region Private Fields
private int _value = 1;
private int _interval = 100;
private Color _circleColor = Color.FromArgb(20, 20, 20);
private bool _autoStart;
private bool _stopped = true;
private float _circleSize = 1.0F;
private int _numberOfCircles = 8;
private int _numberOfVisibleCircles = 8;
private RotationType _rotation = RotationType.Clockwise;
private float _percentage;
private bool _showPercentage;
private bool _showText;
private TextDisplayModes _textDisplay = TextDisplayModes.None;
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the base color for the circles.
/// </summary>
[DefaultValue(typeof(Color), "20, 20, 20")]
[Description("Gets or sets the base color for the circles.")]
[Category("Appearance")]
public Color CircleColor
{
get { return _circleColor; }
set
{
_circleColor = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets a value indicating if the animation should start automatically.
/// </summary>
[DefaultValue(false)]
[Description("Gets or sets a value indicating if the animation should start automatically.")]
[Category("Behavior")]
public bool AutoStart
{
get { return _autoStart; }
set
{
_autoStart = value;
if (_autoStart && !DesignMode)
Start();
else
Stop();
}
}
/// <summary>
/// Gets or sets the scale for the circles raging from 0.1 to 1.0.
/// </summary>
[DefaultValue(1.0F)]
[Description("Gets or sets the scale for the circles raging from 0.1 to 1.0.")]
[Category("Appearance")]
public float CircleSize
{
get { return _circleSize; }
set
{
if (value <= 0.0F)
_circleSize = 0.1F;
else
_circleSize = value > 1.0F ? 1.0F : value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the animation speed.
/// </summary>
[DefaultValue(75)]
[Description("Gets or sets the animation speed.")]
[Category("Behavior")]
public int AnimationSpeed
{
get { return (-_interval + 400) / 4; }
set
{
checked
{
int interval = 400 - (value * 4);
if (interval < 10)
_interval = 10;
else
_interval = interval > 400 ? 400 : interval;
timerAnimation.Interval = _interval;
}
}
}
/// <summary>
/// Gets or sets the number of circles used in the animation.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"><c>NumberOfCircles</c> is out of range.</exception>
[DefaultValue(8)]
[Description("Gets or sets the number of circles used in the animation.")]
[Category("Behavior")]
public int NumberOfCircles
{
get { return _numberOfCircles; }
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("value", "Number of circles must be a positive integer.");
_numberOfCircles = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the number of circles used in the animation.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"><c>NumberOfCircles</c> is out of range.</exception>
[DefaultValue(8)]
[Description("Gets or sets the number of circles used in the animation.")]
[Category("Behavior")]
public int NumberOfVisibleCircles
{
get { return _numberOfVisibleCircles; }
set
{
if (value <= 0 || value > _numberOfCircles)
throw new ArgumentOutOfRangeException("value", "Number of circles must be a positive integer and less than or equal to the number of circles.");
_numberOfVisibleCircles = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets a value indicating if the rotation should be clockwise or counter-clockwise.
/// </summary>
[DefaultValue(RotationType.Clockwise)]
[Description("Gets or sets a value indicating if the rotation should be clockwise or counter-clockwise.")]
[Category("Behavior")]
public RotationType Rotation
{
get { return _rotation; }
set { _rotation = value; }
}
/// <summary>
/// Gets or sets the percentage to show on the control.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"><c>Percentage</c> is out of range.</exception>
[DefaultValue(0)]
[Description("Gets or sets the percentage to show on the control.")]
[Category("Appearance")]
public float Percentage
{
get { return _percentage; }
set
{
if (value < 0 || value > 100)
throw new ArgumentOutOfRangeException("value", "Percentage must be a positive integer between 0 and 100.");
_percentage = value;
}
}
/// <summary>
/// Gets or sets a value indicating if the percentage value should be shown.
/// </summary>
[DefaultValue(false)]
[Description("Gets or sets a value indicating if the percentage value should be shown.")]
[Category("Behavior")]
public bool ShowPercentage
{
get { return _showPercentage; }
set
{
_showPercentage = value;
_textDisplay = _showPercentage
? _textDisplay | TextDisplayModes.Percentage
: _textDisplay & ~TextDisplayModes.Percentage;
Invalidate();
}
}
/// <summary>
/// Gets or sets a value indicating if the control text should be shown.
/// </summary>
[DefaultValue(false)]
[Description("Gets or sets a value indicating if the control text should be shown.")]
[Category("Behavior")]
public bool ShowText
{
get { return _showText; }
set
{
_showText = value;
_textDisplay = _showText
? _textDisplay | TextDisplayModes.Text
: _textDisplay & ~TextDisplayModes.Text;
Invalidate();
}
}
/// <summary>
/// Gets or sets the property that will be shown in the control.
/// </summary>
[DefaultValue(TextDisplayModes.None)]
[Description("Gets or sets the property that will be shown in the control.")]
[Category("Appearance")]
public TextDisplayModes TextDisplay
{
get { return _textDisplay; }
set
{
_textDisplay = value;
_showText = (_textDisplay & TextDisplayModes.Text) == TextDisplayModes.Text;
_showPercentage = (_textDisplay & TextDisplayModes.Percentage) == TextDisplayModes.Percentage;
Invalidate();
}
}
#endregion
#region Public Methods
/// <summary>
/// Starts the animation.
/// </summary>
public void Start()
{
timerAnimation.Interval = _interval;
_stopped = false;
timerAnimation.Start();
}
/// <summary>
/// Stops the animation.
/// </summary>
public void Stop()
{
timerAnimation.Stop();
_value = 1;
_stopped = true;
Invalidate();
}
#endregion
#region Overrides
/// <summary>
/// Override the OnPaint Method
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
float angle = 360.0F / _numberOfCircles;
GraphicsState oldState = e.Graphics.Save();
e.Graphics.TranslateTransform(Width / 2.0F, Height / 2.0F);
e.Graphics.RotateTransform(angle * _value * (int)_rotation);
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int i = 1; i <= _numberOfCircles; i++)
{
int alphaValue = (255.0F * (i / (float)_numberOfVisibleCircles)) > 255.0 ? 0 : (int)(255.0F * (i / (float)_numberOfVisibleCircles));
int alpha = _stopped ? (int)(255.0F * (1.0F / 8.0F)) : alphaValue;
Color drawColor = Color.FromArgb(alpha, _circleColor);
using (SolidBrush brush = new SolidBrush(drawColor))
{
float sizeRate = 4.5F / _circleSize;
float size = Width / sizeRate;
float diff = (Width / 4.5F) - size;
float x = (Width / 9.0F) + diff;
float y = (Height / 9.0F) + diff;
e.Graphics.FillEllipse(brush, x, y, size, size);
e.Graphics.RotateTransform(angle * (int)_rotation);
}
}
e.Graphics.Restore(oldState);
string percent = GetDrawText();
if (!string.IsNullOrEmpty(percent))
{
SizeF textSize = e.Graphics.MeasureString(percent, Font);
float textX = (Width / 2.0F) - (textSize.Width / 2.0F);
float textY = (Height / 2.0F) - (textSize.Height / 2.0F);
StringFormat format = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
RectangleF rectangle = new RectangleF(textX, textY, textSize.Width, textSize.Height);
using (SolidBrush textBrush = new SolidBrush(ForeColor))
{
e.Graphics.DrawString(percent, Font, textBrush, rectangle, format);
}
}
base.OnPaint(e);
}
/// <summary>
/// Override the OnResize Method.
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
SetNewSize();
base.OnResize(e);
}
/// <summary>
/// Override the OnSizeChanged method.
/// </summary>
/// <param name="e"></param>
protected override void OnSizeChanged(EventArgs e)
{
SetNewSize();
base.OnSizeChanged(e);
}
#endregion
#region Private Methods
private string GetDrawText()
{
string percent = string.Format(CultureInfo.CurrentCulture, "{0:0.##} %", _percentage);
if (_showText && _showPercentage)
return string.Format("{0}{1}{2}", percent, Environment.NewLine, Text);
if (_showText)
return Text;
if (_showPercentage)
return percent;
return string.Empty;
}
private void SetNewSize()
{
int size = Math.Max(Width, Height);
Size = new Size(size, size);
}
private void IncreaseValue()
{
if (_value + 1 <= _numberOfCircles)
_value++;
else
_value = 1;
}
#endregion
#region Timer
private void timerAnimation_Tick(object sender, EventArgs e)
{
if (!DesignMode)
{
IncreaseValue();
Invalidate();
}
}
#endregion
}
}