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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;
namespace ViperBytes
{
/// <summary>
/// Special progress bar is a kind of iTunes bar
/// Text member: The token {0} will be replaced with the difference between starting and ending value.
/// </summary>
public class SpecialProgressBar : Control
{
#region SpecialProgressBarStyle enumeration
/// <summary>
/// Lists possible styles
/// </summary>
public enum SpecialProgressBarStyle
{
Square,
Round,
Peaked,
Slang
}
#endregion
#region private fields
/// <summary>
/// Style of progress bar ends
/// </summary>
private SpecialProgressBarStyle style = SpecialProgressBarStyle.Square;
/// <summary>
/// Color of border
/// </summary>
private Color borderColor = Color.Gray;
/// <summary>
/// Light fill color of gradient
/// </summary>
private Color lightFillColor = Color.LightGreen;
/// <summary>
/// Dark fill color of gradient
/// </summary>
private Color darkFillColor = Color.DarkGreen;
/// <summary>
/// Light background color of gradient
/// </summary>
private Color lightBackColor = Color.LightGray;
/// <summary>
/// Dark background color of gradient
/// </summary>
private Color darkBackColor = Color.DarkGray;
/// <summary>
/// Number of separators
/// </summary>
private int separators = 10;
/// <summary>
/// Starting value of fillage
/// </summary>
private int startValue = 0;
/// <summary>
/// Ending value of fillage
/// </summary>
private int endValue = 0;
#endregion
#region public events
/// <summary>
/// Fired, when border color changed
/// </summary>
public event EventHandler BorderColorChanged;
/// <summary>
/// Fired, when light fill color changed
/// </summary>
public event EventHandler LightFillColorChanged;
/// <summary>
/// Fired, when dark fill color changed
/// </summary>
public event EventHandler DarkFillColorChanged;
/// <summary>
/// Fired, when light background color changed
/// </summary>
public event EventHandler LightBackColorChanged;
/// <summary>
/// Fired, when dark background color changed
/// </summary>
public event EventHandler DarkBackColorChanged;
/// <summary>
/// Fired, when number of separators changed
/// </summary>
public event EventHandler SeparatorsChanged;
/// <summary>
/// Fired, when starting value changed
/// </summary>
public event EventHandler StartValueChanged;
/// <summary>
/// Fired, when ending value changed
/// </summary>
public event EventHandler EndValueChanged;
#endregion
#region properties
/// <summary>
/// Specifyes the ending's style
/// </summary>
[DefaultValue(typeof(SpecialProgressBar.SpecialProgressBarStyle), "Square")]
public SpecialProgressBarStyle Style
{
get
{
return this.style;
}
set
{
if (this.style != value)
{
this.style = value;
Invalidate();
OnStyleChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes the border color
/// </summary>
[DefaultValue(typeof(Color), "Gray")]
public Color BorderColor
{
get
{
return this.borderColor;
}
set
{
if (this.borderColor != value)
{
this.borderColor = value;
Invalidate();
OnBorderColorChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes the light fill color of gradient
/// </summary>
[DefaultValue(typeof(Color), "LightGreen")]
public Color LightFillColor
{
get
{
return this.lightFillColor;
}
set
{
if (this.lightFillColor != value)
{
this.lightFillColor = value;
Invalidate();
OnLightFillColorChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes the dark fill color of gradient
/// </summary>
[DefaultValue(typeof(Color), "DarkGreen")]
public Color DarkFillColor
{
get
{
return this.darkFillColor;
}
set
{
if (this.darkFillColor != value)
{
this.darkFillColor = value;
Invalidate();
OnDarkFillColorChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes the light background color of gradient
/// </summary>
[DefaultValue(typeof(Color), "LightGray")]
public Color LightBackColor
{
get
{
return this.lightBackColor;
}
set
{
if (this.lightBackColor != value)
{
this.lightBackColor = value;
Invalidate();
OnLightBackColorChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes the dark background color of gradient
/// </summary>
[DefaultValue(typeof(Color), "DarkGray")]
public Color DarkBackColor
{
get
{
return this.darkBackColor;
}
set
{
if (this.darkBackColor != value)
{
this.darkBackColor = value;
Invalidate();
OnDarkBackColorChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes the number of separators
/// </summary>
[DefaultValue(10)]
public int Separators
{
get
{
return this.separators;
}
set
{
if (this.separators != value)
{
this.separators = value;
Invalidate();
OnSeparatorsChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes starting value
/// </summary>
[DefaultValue(0)]
public int StartValue
{
get
{
return this.startValue;
}
set
{
if (this.startValue != value)
{
this.startValue = value;
Invalidate();
OnStartValueChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Specifyes ending value
/// </summary>
[DefaultValue(0)]
public int EndValue
{
get
{
return this.endValue;
}
set
{
if (this.endValue != value)
{
this.endValue = value;
Invalidate();
OnEndValueChanged(EventArgs.Empty);
}
}
}
#endregion
#region Constructor
/// <summary>
/// Constructor
/// </summary>
public SpecialProgressBar()
: base()
{
Size = new Size(320, 32);
DoubleBuffered = true;
Text = "{0} %";
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="text">Text member</param>
public SpecialProgressBar(string text)
: base(text)
{
Size = new Size(320, 32);
DoubleBuffered = true;
Text = "{0} %";
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="parent">Parent control</param>
/// <param name="text">Text members</param>
public SpecialProgressBar(Control parent, string text)
: base(parent, text)
{
Size = new Size(320, 32);
DoubleBuffered = true;
Text = "{0} %";
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="text">Text member</param>
/// <param name="left">X-coordinate</param>
/// <param name="top">Y-coordinate</param>
/// <param name="width">Width member</param>
/// <param name="height">Height member</param>
public SpecialProgressBar(string text, int left, int top, int width, int height)
: base(text, left, top, width, height)
{
DoubleBuffered = true;
Text = "{0} %";
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="parent">Parent control</param>
/// <param name="text">Text member</param>
/// <param name="left">X-coordinate</param>
/// <param name="top">Y-coordinate</param>
/// <param name="width">Width member</param>
/// <param name="height">Height member</param>
public SpecialProgressBar(Control parent, string text, int left, int top, int width, int height)
: base(parent, text, left, top, width, height)
{
DoubleBuffered = true;
Text = "{0} %";
}
#endregion
#region protected members
/// <summary>
/// Fired, when border color changed
/// </summary>
protected void OnBorderColorChanged(EventArgs e)
{
if (BorderColorChanged != null)
BorderColorChanged(this, e);
}
/// <summary>
/// Fired, when light fill color changed
/// </summary>
protected void OnLightFillColorChanged(EventArgs e)
{
if (LightFillColorChanged != null)
LightFillColorChanged(this, e);
}
/// <summary>
/// Fired, when dark fill color changed
/// </summary>
protected void OnDarkFillColorChanged(EventArgs e)
{
if (DarkFillColorChanged != null)
DarkFillColorChanged(this, e);
}
/// <summary>
/// Fired, when light background color changed
/// </summary>
protected void OnLightBackColorChanged(EventArgs e)
{
if (LightBackColorChanged != null)
LightBackColorChanged(this, e);
}
/// <summary>
/// Fired, when dark background color changed
/// </summary>
protected void OnDarkBackColorChanged(EventArgs e)
{
if (DarkBackColorChanged != null)
DarkBackColorChanged(this, e);
}
/// <summary>
/// Fired, when number of separators changed
/// </summary>
protected void OnSeparatorsChanged(EventArgs e)
{
if (SeparatorsChanged != null)
SeparatorsChanged(this, e);
}
/// <summary>
/// Fired, when starting value changed
/// </summary>
protected void OnStartValueChanged(EventArgs e)
{
if (StartValueChanged != null)
StartValueChanged(this, e);
}
/// <summary>
/// Fired, when ending value changed
/// </summary>
protected void OnEndValueChanged(EventArgs e)
{
if (EndValueChanged != null)
EndValueChanged(this, e);
}
/// <summary>
/// Fired, when size changed
/// </summary>
/// <param name="e"></param>
protected override void OnSizeChanged(EventArgs e)
{
if (Height > Width)
Width = Height;
base.OnSizeChanged(e);
}
/// <summary>
/// Fired, when text changed
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
Invalidate();
base.OnTextChanged(e);
}
/// <summary>
/// Fired on paint event
/// </summary>
/// <param name="e">Event arguments</param>
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = new Rectangle(1, 1, Width - 2, Height - 2);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
GraphicsPath path = GetPath(rect);
// Draw back gradient
using (LinearGradientBrush brush = new LinearGradientBrush(rect, LightBackColor, DarkBackColor, LinearGradientMode.Vertical))
{
g.FillPath(brush, path);
}
if (EndValue - StartValue != 0)
{
// Draw fill gradient
using (LinearGradientBrush brush = new LinearGradientBrush(rect, LightFillColor, DarkFillColor, LinearGradientMode.Vertical))
{
RectangleF clip = new RectangleF(rect.X + (float)rect.Width * (float)StartValue / 100F, rect.Y, (float)rect.Width * (float)(EndValue - StartValue) / 100F, rect.Height);
g.SetClip(clip);
g.FillPath(brush, path);
g.ResetClip();
}
}
using (Pen pen = new Pen(BorderColor, 1))
{
if (EndValue - StartValue != 0)
{
float startSplit = rect.X + (float)rect.Width * (float)StartValue / 100F - 0.5F;
float endSplit = rect.X + (float)rect.Width * (float)EndValue / 100F - 0.5F;
g.SetClip(path);
g.DrawLine(pen, startSplit, rect.Y - 0.5F, startSplit, rect.Y + rect.Height - 0.5F);
g.DrawLine(pen, endSplit, rect.Y - 0.5F, endSplit, rect.Y + rect.Height - 0.5F);
g.ResetClip();
}
// draw Border
Matrix matrix = new Matrix();
matrix.Translate(-0.5F, -0.5F);
path.Transform(matrix);
g.DrawPath(pen, path);
}
if (Separators > 0)
{
using (Pen pen1 = new Pen(Color.FromArgb(48, Color.Black), 1))
{
using (Pen pen2 = new Pen(Color.FromArgb(48, Color.White), 1))
{
float step = (float)rect.Width / (float)(Separators + 1);
g.SetClip(path);
for (float split = rect.X + step - 0.5F; split < rect.X + rect.Width - 0.5F; split += step)
{
g.DrawLine(pen1, split - 0.5F, rect.Y - 0.5F, split - 0.5F, rect.Y + rect.Height - 0.5F);
g.DrawLine(pen2, split + 0.5F, rect.Y - 0.5F, split + 0.5F, rect.Y + rect.Height - 0.5F);
}
g.ResetClip();
}
}
}
// draw light
using (SolidBrush brush = new SolidBrush(Color.FromArgb(48, Color.White)))
{
g.SetClip(path);
g.FillRectangle(brush, new RectangleF(rect.X, rect.Y, rect.Width, rect.Height / 2F));
g.ResetClip();
}
string text;
if (Text.Contains("{0}"))
text = Text.Replace("{0}", ((int)Math.Abs(EndValue - StartValue)).ToString());
else
text = Text;
if (text.Length > 0)
{
SizeF fontSize = g.MeasureString(text, Font);
RectangleF fontRect = new RectangleF(
rect.X + rect.Width / 2F - fontSize.Width / 2F,
rect.Y + rect.Height / 2F - fontSize.Height / 2F,
fontSize.Width,
fontSize.Height);
g.DrawString(text, Font, new SolidBrush(ForeColor), fontRect);
}
base.OnPaint(e);
}
#endregion
#region private members
private GraphicsPath GetPath(RectangleF rect)
{
GraphicsPath path = new GraphicsPath();
switch (Style)
{
case SpecialProgressBarStyle.Square:
path.AddRectangle(rect);
break;
case SpecialProgressBarStyle.Round:
path.AddArc(new RectangleF(rect.X, rect.Y, rect.Height, rect.Height), 90, 180);
path.AddArc(new RectangleF(rect.X + rect.Width - rect.Height, rect.Y, rect.Height, rect.Height), 270, 180);
path.CloseFigure();
break;
case SpecialProgressBarStyle.Peaked:
{
PointF[] points = new PointF[] {
new PointF(rect.X, rect.Y + rect.Height / 2F),
new PointF(rect.X + rect.Height / 2F, rect.Y),
new PointF(rect.X + rect.Width - rect.Height / 2F, rect.Y),
new PointF(rect.X + rect.Width, rect.Y + rect.Height / 2F),
new PointF(rect.X + rect.Width - rect.Height / 2F, rect.Y + rect.Height),
new PointF(rect.X + rect.Height / 2F, rect.Y + rect.Height)
};
path.AddLines(points);
path.CloseFigure();
}
break;
case SpecialProgressBarStyle.Slang:
{
PointF[] points = new PointF[] {
new PointF(rect.X, rect.Y),
new PointF(rect.X + rect.Width - rect.Height / 2F, rect.Y),
new PointF(rect.X + rect.Width, rect.Y + rect.Height),
new PointF(rect.X + rect.Height / 2F, rect.Y + rect.Height)
};
path.AddLines(points);
path.CloseFigure();
}
break;
}
return path;
}
#endregion
}
}
|