-
Notifications
You must be signed in to change notification settings - Fork 11
/
ExtendedToolStripStatusLabel.cs
499 lines (401 loc) · 13 KB
/
ExtendedToolStripStatusLabel.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
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
using ComponentFactory.Krypton.Toolkit;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ExtendedControls.ExtendedToolkit.ToolstripControls
{
[ToolboxBitmap(typeof(ToolStripStatusLabel)), ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.StatusStrip | ToolStripItemDesignerAvailability.ToolStrip)]
public class ExtendedToolStripStatusLabel : ToolStripStatusLabel
{
#region Variables
#region Krypton
IPalette _palette;
IRenderer _renderer;
// This may not be needed, but oh well...
KryptonPalette _kryptonPalette;
#endregion
bool _alert, _enableBlinking;
Color _textColour, _backGradient1, _backGradient2, _textGlow, _alertColour1, _alertColour2, _alertTextColour;
Font _textTypeface;
LinearGradientMode _linearGradientMode;
int _textGlowSpread, _flashInterval;
Timer _alertFlashTimer;
long _blinkDuration;
#endregion
#region Properties
/// <summary>
/// Gets or sets a value indicating whether this <see cref="ExtendedToolStripStatusLabel"/> is alert.
/// </summary>
/// <value>
/// <c>true</c> if alert; otherwise, <c>false</c>.
/// </value>
[DefaultValue(false), Description("Alerts the user."), Category("Appearance")]
public bool Alert
{
get
{
return _alert;
}
set
{
_alert = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether [enable blinking].
/// </summary>
/// <value>
/// <c>true</c> if [enable blinking]; otherwise, <c>false</c>.
/// </value>
[DefaultValue(false), Description("Enables a blinking mode."), Category("Blinking Settings")]
public bool EnableBlinking
{
get
{
return _enableBlinking;
}
set
{
_enableBlinking = value;
}
}
/// <summary>
/// Gets or sets the alert colour one.
/// </summary>
/// <value>
/// The alert colour one.
/// </value>
[DefaultValue(typeof(Color), "White"), Description("Defined alert first colour."), Category("Blinking Settings")]
public Color AlertColourOne
{
get
{
return _alertColour1;
}
set
{
_alertColour1 = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the alert colour two.
/// </summary>
/// <value>
/// The alert colour two.
/// </value>
[DefaultValue(typeof(Color), "Black"), Description("Defined alert second colour."), Category("Blinking Settings")]
public Color AlertColourTwo
{
get
{
return _alertColour2;
}
set
{
_alertColour2 = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the alert text colour.
/// </summary>
/// <value>
/// The alert text colour.
/// </value>
[DefaultValue(typeof(Color), "Red"), Description("Defined alert text colour."), Category("Blinking Settings")]
public Color AlertTextColour
{
get
{
return _alertTextColour;
}
set
{
_alertTextColour = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the gradient colour one.
/// </summary>
/// <value>
/// The gradient colour one.
/// </value>
[DefaultValue(typeof(Color), "White"), Description("The first gradient colour."), Category("Appearance")]
public Color GradientColourOne
{
get
{
return _backGradient1;
}
set
{
_backGradient1 = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the gradient colour two.
/// </summary>
/// <value>
/// The gradient colour two.
/// </value>
[DefaultValue(typeof(Color), "Blue"), Description("The second gradient colour."), Category("Appearance")]
public Color GradientColourTwo
{
get
{
return _backGradient2;
}
set
{
_backGradient2 = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the text glow colour.
/// </summary>
/// <value>
/// The text glow colour.
/// </value>
[DefaultValue(typeof(Color), "White"), Description("The text glow colour."), Category("Appearance")]
public Color TextGlow
{
get
{
return _textGlow;
}
set
{
_textGlow = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the gradient mode.
/// </summary>
/// <value>
/// The gradient mode.
/// </value>
[DefaultValue(typeof(LinearGradientMode), "ForwardDiagonal"), Description("Gradient mode"), Category("Appearance")]
public LinearGradientMode GradientMode
{
get
{
return _linearGradientMode;
}
set
{
_linearGradientMode = value;
Invalidate();
}
}
/// <summary>
/// Gets or sets the text glow spread.
/// </summary>
/// <value>
/// The text glow spread.
/// </value>
[DefaultValue(5), Description("The text glow spread"), Category("Appearance")]
public int TextGlowSpread
{
get
{
return _textGlowSpread;
}
set
{
_textGlowSpread = value;
}
}
/// <summary>
/// Gets or sets the alert blink interval.
/// </summary>
/// <value>
/// The alert blink interval.
/// </value>
[DefaultValue(256), Description("The blink interval."), Category("Blinking Settings")]
public int AlertBlinkInterval
{
get
{
return _flashInterval;
}
set
{
_flashInterval = value;
}
}
/// <summary>
/// Gets or sets the duration of the blink.
/// </summary>
/// <value>
/// The duration of the blink.
/// </value>
[DefaultValue(10), Description("Defines how long the blinking lasts for."), Category("Blinking Settings")]
public long BlinkDuration
{
get
{
return _blinkDuration;
}
set
{
_blinkDuration = value;
}
}
#endregion
#region Constructors
public ExtendedToolStripStatusLabel()
{
Alert = false;
AlertColourOne = Color.White;
AlertColourTwo = Color.Black;
AlertTextColour = Color.Red;
GradientColourOne = Color.Empty;
GradientColourTwo = Color.Empty;
TextGlow = Color.White;
GradientMode = LinearGradientMode.ForwardDiagonal;
TextGlowSpread = 5;
AlertBlinkInterval = 256;
BlinkDuration = 10;
}
//public ExtendedToolStripStatusLabel(bool enableBlinking)
//{
// if (enableBlinking)
// {
// _alertFlashTimer = new Timer();
// _alertFlashTimer.Interval = AlertBlinkInterval;
// if (enableBlinking)
// {
// _alertFlashTimer.Start();
// _alertFlashTimer.Tick += AlertFlashTimer_Tick;
// base.ForeColor = AlertTextColour;
// }
// }
//}
//private void AlertFlashTimer_Tick(object sender, EventArgs e)
//{
// if (AlertColourOne != Color.Empty && AlertColourTwo != Color.Empty)
// {
// if (BackColor == AlertColourOne)
// {
// BackColor = AlertColourTwo;
// }
// else
// {
// BackColor = AlertColourOne;
// }
// }
//}
#endregion
#region Overrides
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Color BackColor { get; set; }
/// <summary>
/// Raises the <see cref="E:System.Windows.Forms.ToolStripItem.Paint" /> event.
/// </summary>
/// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
// Set a graphics variable
Graphics g = e.Graphics;
// Rectangle variable
Rectangle r = new Rectangle(0, 0, Width, Height);
if (BackColor != Color.Empty)
{
// Fill the background with a solid colour
using (SolidBrush solidBrush = new SolidBrush(BackColor))
{
g.FillRectangle(solidBrush, r);
}
}
else if (GradientColourOne != Color.Empty || GradientColourTwo != Color.Empty)
{
// Fill the background with a gradient colour
using (LinearGradientBrush lgb = new LinearGradientBrush(r, GradientColourOne, GradientColourTwo, GradientMode))
{
g.FillRectangle(lgb, r);
}
}
// Text rendering
if (ForeColor != Color.Empty)
{
g.TextRenderingHint = TextRenderingHint.AntiAlias | TextRenderingHint.ClearTypeGridFit;
// Preserve user font settings
Font typeface = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit);
SolidBrush brush = new SolidBrush(ForeColor);
// Draw the text
g.DrawString(Text, typeface, brush, 0, 0);
}
//base.OnPaint(e);
}
#endregion
#region Methods
public void SetTextColour(Color textColour)
{
if (_palette != null)
{
}
}
/// <summary>
/// Blinks the label.
/// </summary>
public async void BlinkLabel(long blinkDuration)
{
var sw = Stopwatch.StartNew();
var fgc = ForeColor;
var bgc = BackColor;
while (sw.ElapsedMilliseconds < blinkDuration)
{
await Task.Delay(_flashInterval);
base.BackColor = base.BackColor == AlertColourOne ? AlertColourTwo : AlertColourOne;
base.ForeColor = AlertTextColour;
Invalidate();
}
BackColor = bgc;
ForeColor = fgc;
Invalidate();
sw.Stop();
}
public async void SoftBlink(Color alertColour1, Color alertColour2, Color alertTextColour, short cycleInterval, bool bkClr, long blinkDuration)
{
var sw = Stopwatch.StartNew();
var fgc = ForeColor;
var bgc = BackColor;
short halfCycle = (short)Math.Round(cycleInterval * 0.5);
while (sw.ElapsedMilliseconds < blinkDuration)
{
await Task.Delay(1);
var n = sw.ElapsedMilliseconds % cycleInterval;
var per = (double)Math.Abs(n - halfCycle) / halfCycle;
var red = (short)Math.Round((alertColour2.R - alertColour1.R) * per) + alertColour1.R;
var grn = (short)Math.Round((alertColour2.G - alertColour1.G) * per) + alertColour1.G;
var blw = (short)Math.Round((alertColour2.B - alertColour1.B) * per) + alertColour1.B;
var clr = Color.FromArgb(red, grn, blw);
if (bkClr)
{
base.BackColor = clr;
}
else
{
base.ForeColor = clr;
}
Invalidate();
}
BackColor = bgc;
ForeColor = fgc;
Invalidate();
sw.Stop();
}
#endregion
}
}