1
+ using System ;
2
+ using System . ComponentModel ;
3
+ using System . Windows . Forms ;
4
+ using System . Drawing ;
5
+
6
+ namespace ColorProgressBar
7
+ {
8
+ [ Description ( "Color Progress Bar" ) ]
9
+ [ ToolboxBitmap ( typeof ( ProgressBar ) ) ]
10
+ [ Designer ( typeof ( ColorProgressBarDesigner ) ) ]
11
+ public class ColorProgressBar : System . Windows . Forms . Control
12
+ {
13
+
14
+ private int _Value = 0 ;
15
+ private int _Minimum = 0 ;
16
+ private int _Maximum = 100 ;
17
+ private int _Step = 10 ;
18
+
19
+ private Color _BarColor = Color . FromArgb ( 255 , 128 , 128 ) ;
20
+ private Color _BorderColor = Color . Black ;
21
+
22
+ public enum FillStyles
23
+ {
24
+ Solid ,
25
+ Dashed
26
+ }
27
+
28
+ public ColorProgressBar ( )
29
+ {
30
+ base . Size = new Size ( 150 , 15 ) ;
31
+ SetStyle ( ControlStyles . AllPaintingInWmPaint | ControlStyles . ResizeRedraw | ControlStyles . DoubleBuffer , true ) ;
32
+ }
33
+
34
+ [ Description ( "ColorProgressBar color" ) ]
35
+ [ Category ( "ColorProgressBar" ) ]
36
+ public Color BarColor
37
+ {
38
+ get
39
+ {
40
+ return _BarColor ;
41
+ }
42
+ set
43
+ {
44
+ _BarColor = value ;
45
+ this . Invalidate ( ) ;
46
+ }
47
+ }
48
+
49
+ [ Description ( "The current value for the ColorProgressBar, in the range specified by the Minimum and Maximum properties." ) ]
50
+ [ Category ( "ColorProgressBar" ) ]
51
+ [ RefreshProperties ( RefreshProperties . All ) ]
52
+ public int Value
53
+ {
54
+ get
55
+ {
56
+ return _Value ;
57
+ }
58
+ set
59
+ {
60
+ if ( value < _Minimum )
61
+ {
62
+ throw new ArgumentException ( "'" + value + "' is not a valid value for 'Value'.\n " +
63
+ "'Value' must be between 'Minimum' and 'Maximum'." ) ;
64
+ }
65
+
66
+ if ( value > _Maximum )
67
+ {
68
+ throw new ArgumentException ( "'" + value + "' is not a valid value for 'Value'.\n " +
69
+ "'Value' must be between 'Minimum' and 'Maximum'." ) ;
70
+ }
71
+
72
+ _Value = value ;
73
+ this . Invalidate ( ) ;
74
+ }
75
+ }
76
+
77
+ [ Description ( "The lower bound of the range this ColorProgressbar is working with." ) ]
78
+ [ Category ( "ColorProgressBar" ) ]
79
+ [ RefreshProperties ( RefreshProperties . All ) ]
80
+ public int Minimum
81
+ {
82
+ get
83
+ {
84
+ return _Minimum ;
85
+ }
86
+ set
87
+ {
88
+ _Minimum = value ;
89
+
90
+ if ( _Minimum > _Maximum )
91
+ _Maximum = _Minimum ;
92
+ if ( _Minimum > _Value )
93
+ _Value = _Minimum ;
94
+
95
+ this . Invalidate ( ) ;
96
+ }
97
+ }
98
+
99
+ [ Description ( "The uppper bound of the range this ColorProgressbar is working with." ) ]
100
+ [ Category ( "ColorProgressBar" ) ]
101
+ [ RefreshProperties ( RefreshProperties . All ) ]
102
+ public int Maximum
103
+ {
104
+ get
105
+ {
106
+ return _Maximum ;
107
+ }
108
+ set
109
+ {
110
+ _Maximum = value ;
111
+
112
+ if ( _Maximum < _Value )
113
+ _Value = _Maximum ;
114
+ if ( _Maximum < _Minimum )
115
+ _Minimum = _Maximum ;
116
+
117
+ this . Invalidate ( ) ;
118
+ }
119
+ }
120
+
121
+ [ Description ( "The amount to jump the current value of the control by when the Step() method is called." ) ]
122
+ [ Category ( "ColorProgressBar" ) ]
123
+ public int Step
124
+ {
125
+ get
126
+ {
127
+ return _Step ;
128
+ }
129
+ set
130
+ {
131
+ _Step = value ;
132
+ this . Invalidate ( ) ;
133
+ }
134
+ }
135
+
136
+ [ Description ( "The border color of ColorProgressBar" ) ]
137
+ [ Category ( "ColorProgressBar" ) ]
138
+ public Color BorderColor
139
+ {
140
+ get
141
+ {
142
+ return _BorderColor ;
143
+ }
144
+ set
145
+ {
146
+ _BorderColor = value ;
147
+ this . Invalidate ( ) ;
148
+ }
149
+ }
150
+
151
+ ///
152
+ /// <summary>Call the PerformStep() method to increase the value displayed by the amount set in the Step property</summary>
153
+ ///
154
+ public void PerformStep ( )
155
+ {
156
+ if ( _Value < _Maximum )
157
+ _Value += _Step ;
158
+ else
159
+ _Value = _Maximum ;
160
+
161
+ this . Invalidate ( ) ;
162
+ }
163
+
164
+ ///
165
+ /// <summary>Call the PerformStepBack() method to decrease the value displayed by the amount set in the Step property</summary>
166
+ ///
167
+ public void PerformStepBack ( )
168
+ {
169
+ if ( _Value > _Minimum )
170
+ _Value -= _Step ;
171
+ else
172
+ _Value = _Minimum ;
173
+
174
+ this . Invalidate ( ) ;
175
+ }
176
+
177
+ ///
178
+ /// <summary>Call the Increment() method to increase the value displayed by an integer you specify</summary>
179
+ ///
180
+ public void Increment ( int value )
181
+ {
182
+ if ( _Value < _Maximum )
183
+ _Value += value ;
184
+ else
185
+ _Value = _Maximum ;
186
+
187
+ this . Invalidate ( ) ;
188
+ }
189
+
190
+ //
191
+ // <summary>Call the Decrement() method to decrease the value displayed by an integer you specify</summary>
192
+ //
193
+ public void Decrement ( int value )
194
+ {
195
+ if ( _Value > _Minimum )
196
+ _Value -= value ;
197
+ else
198
+ _Value = _Minimum ;
199
+
200
+ this . Invalidate ( ) ;
201
+ }
202
+
203
+ protected override void OnPaint ( System . Windows . Forms . PaintEventArgs e )
204
+ {
205
+ //
206
+ // Check for value
207
+ //
208
+ if ( _Maximum == _Minimum || _Value == 0 )
209
+ {
210
+ // Draw border only and exit;
211
+ DrawBorder ( e . Graphics ) ;
212
+ return ;
213
+ }
214
+
215
+ //
216
+ // The following is the width of the bar. This will vary with each value.
217
+ //
218
+ int fillWidth = ( this . Width * _Value ) / ( _Maximum - _Minimum ) ;
219
+
220
+ //
221
+ // Rectangles for upper and lower half of bar
222
+ //
223
+ Rectangle rect = new Rectangle ( 0 , 0 , fillWidth , this . Height ) ;
224
+
225
+ //
226
+ // The brush
227
+ //
228
+ SolidBrush brush = new SolidBrush ( _BarColor ) ;
229
+ e . Graphics . FillRectangle ( brush , rect ) ;
230
+ brush . Dispose ( ) ;
231
+
232
+ //
233
+ // Draw border and exit
234
+ DrawBorder ( e . Graphics ) ;
235
+ }
236
+
237
+ protected void DrawBorder ( Graphics g )
238
+ {
239
+ Rectangle borderRect = new Rectangle ( 0 , 0 ,
240
+ ClientRectangle . Width - 1 , ClientRectangle . Height - 1 ) ;
241
+ g . DrawRectangle ( new Pen ( _BorderColor , 1 ) , borderRect ) ;
242
+ }
243
+ }
244
+ }
0 commit comments