forked from ukushu/TextProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextProgressBar.cs
More file actions
175 lines (146 loc) · 5.11 KB
/
TextProgressBar.cs
File metadata and controls
175 lines (146 loc) · 5.11 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ProgressBarSample
{
public enum ProgressBarDisplayMode
{
NoText,
Percentage,
CurrProgress,
CustomText,
TextAndPercentage,
TextAndCurrProgress
}
public class TextProgressBar : ProgressBar
{
[Description("Font of the text on ProgressBar"), Category("Additional Options")]
public Font TextFont { get; set; } = new Font(FontFamily.GenericSerif, 11, FontStyle.Bold|FontStyle.Italic);
private SolidBrush _textColourBrush = (SolidBrush) Brushes.Black;
[Category("Additional Options")]
public Color TextColor {
get {
return _textColourBrush.Color;
}
set
{
_textColourBrush.Dispose();
_textColourBrush = new SolidBrush(value);
}
}
private SolidBrush _progressColourBrush = (SolidBrush) Brushes.LightGreen;
[Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public Color ProgressColor
{
get
{
return _progressColourBrush.Color;
}
set
{
_progressColourBrush.Dispose();
_progressColourBrush = new SolidBrush(value);
}
}
private ProgressBarDisplayMode _visualMode = ProgressBarDisplayMode.CurrProgress;
[Category("Additional Options"), Browsable(true)]
public ProgressBarDisplayMode VisualMode {
get {
return _visualMode;
}
set
{
_visualMode = value;
Invalidate();//redraw component after change value from VS Properties section
}
}
private string _text = string.Empty;
[Description("If it's empty, % will be shown"), Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string CustomText
{
get
{
return _text;
}
set
{
_text = value;
Invalidate();//redraw component after change value from VS Properties section
}
}
private string _textToDraw
{
get
{
string text = CustomText;
switch (VisualMode)
{
case (ProgressBarDisplayMode.Percentage):
text = _percentageStr;
break;
case (ProgressBarDisplayMode.CurrProgress):
text = _currProgressStr;
break;
case (ProgressBarDisplayMode.TextAndCurrProgress):
text = $"{CustomText}: {_currProgressStr}";
break;
case (ProgressBarDisplayMode.TextAndPercentage):
text = $"{CustomText}: {_percentageStr}";
break;
}
return text;
}
set { }
}
private string _percentageStr { get { return $"{(int)((float)Value - Minimum) / ((float)Maximum - Minimum) * 100 } %"; } }
private string _currProgressStr {
get
{
return $"{Value}/{Maximum}";
}
}
public TextProgressBar()
{
Value = Minimum;
FixComponentBlinking();
}
private void FixComponentBlinking()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawProgressBar(g);
DrawStringIfNeeded(g);
}
private void DrawProgressBar(Graphics g)
{
Rectangle rect = ClientRectangle;
ProgressBarRenderer.DrawHorizontalBar(g, rect);
rect.Inflate(-3, -3);
if (Value > 0)
{
Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
g.FillRectangle(_progressColourBrush, clip);
}
}
private void DrawStringIfNeeded(Graphics g)
{
if (VisualMode != ProgressBarDisplayMode.NoText)
{
string text = _textToDraw;
SizeF len = g.MeasureString(text, TextFont);
Point location = new Point(((Width / 2) - (int)len.Width / 2), ((Height / 2) - (int)len.Height / 2));
g.DrawString(text, TextFont, (Brush)_textColourBrush, location);
}
}
public new void Dispose()
{
_textColourBrush.Dispose();
_progressColourBrush.Dispose();
base.Dispose();
}
}
}