141 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.ComponentModel;
|
|
|
3 |
using System.Drawing;
|
|
|
4 |
using System.Drawing.Drawing2D;
|
|
|
5 |
using System.Windows.Forms;
|
|
|
6 |
using System.Diagnostics;
|
|
|
7 |
|
|
|
8 |
namespace NITNavComm {
|
|
|
9 |
|
|
|
10 |
/// <summary>
|
|
|
11 |
/// The LEDBulb is a .Net control for Windows Forms that emulates an
|
|
|
12 |
/// LED light with two states On and Off. The purpose of the control is to
|
|
|
13 |
/// provide a sleek looking representation of an LED light that is sizable,
|
|
|
14 |
/// has a transparent background and can be set to different colors.
|
|
|
15 |
/// </summary>
|
142 |
pfowler |
16 |
public partial class LedLight : UserControl {
|
141 |
pfowler |
17 |
|
|
|
18 |
#region Public and Private Members
|
|
|
19 |
|
|
|
20 |
private Color _color;
|
|
|
21 |
private bool _on = true;
|
|
|
22 |
private Color _reflectionColor = Color.FromArgb(180, 255, 255, 255);
|
|
|
23 |
private Color[] _surroundColor = new Color[] { Color.FromArgb(0, 255, 255, 255) };
|
|
|
24 |
private Timer _timer = new Timer();
|
147 |
pfowler |
25 |
private Timer _flash = new Timer();
|
141 |
pfowler |
26 |
|
|
|
27 |
/// <summary>
|
|
|
28 |
/// Gets or Sets the color of the LED light
|
|
|
29 |
/// </summary>
|
|
|
30 |
[DefaultValue(typeof(Color), "153, 255, 54")]
|
|
|
31 |
public Color Color {
|
|
|
32 |
get { return _color; }
|
|
|
33 |
set {
|
|
|
34 |
_color = value;
|
|
|
35 |
this.DarkColor = ControlPaint.Dark(_color);
|
|
|
36 |
this.DarkDarkColor = ControlPaint.DarkDark(_color);
|
|
|
37 |
this.Invalidate(); // Redraw the control
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/// <summary>
|
|
|
42 |
/// Dark shade of the LED color used for gradient
|
|
|
43 |
/// </summary>
|
|
|
44 |
public Color DarkColor { get; protected set; }
|
|
|
45 |
|
|
|
46 |
/// <summary>
|
|
|
47 |
/// Very dark shade of the LED color used for gradient
|
|
|
48 |
/// </summary>
|
|
|
49 |
public Color DarkDarkColor { get; protected set; }
|
|
|
50 |
|
147 |
pfowler |
51 |
public bool Flash {
|
|
|
52 |
get { return _flash.Enabled; }
|
|
|
53 |
set { _flash.Enabled = true; }
|
|
|
54 |
}
|
|
|
55 |
|
141 |
pfowler |
56 |
/// <summary>
|
|
|
57 |
/// Gets or Sets whether the light is turned on
|
|
|
58 |
/// </summary>
|
|
|
59 |
public bool On {
|
|
|
60 |
get { return _on; }
|
|
|
61 |
set { _on = value; this.Invalidate(); }
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
#endregion
|
|
|
65 |
|
|
|
66 |
#region Constructor
|
|
|
67 |
|
|
|
68 |
public LedLight() {
|
|
|
69 |
SetStyle(ControlStyles.DoubleBuffer
|
|
|
70 |
| ControlStyles.AllPaintingInWmPaint
|
|
|
71 |
| ControlStyles.ResizeRedraw
|
|
|
72 |
| ControlStyles.UserPaint
|
|
|
73 |
| ControlStyles.SupportsTransparentBackColor, true);
|
|
|
74 |
|
|
|
75 |
this.Color = Color.FromArgb(255, 153, 255, 54);
|
|
|
76 |
_timer.Tick += new EventHandler(
|
|
|
77 |
(object sender, EventArgs e) => { this.On = !this.On; }
|
|
|
78 |
);
|
147 |
pfowler |
79 |
|
|
|
80 |
_flash.Interval = 500;
|
|
|
81 |
_flash.Tick += new EventHandler(
|
|
|
82 |
(object sender, EventArgs e) => { this._flash.Enabled = false; }
|
|
|
83 |
);
|
141 |
pfowler |
84 |
}
|
|
|
85 |
|
|
|
86 |
#endregion
|
|
|
87 |
|
|
|
88 |
#region Methods
|
|
|
89 |
|
|
|
90 |
/// <summary>
|
|
|
91 |
/// Handles the Paint event for this UserControl
|
|
|
92 |
/// </summary>
|
|
|
93 |
protected override void OnPaint(PaintEventArgs e){
|
|
|
94 |
// Create an offscreen graphics object for double buffering
|
|
|
95 |
Bitmap offScreenBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
|
|
|
96 |
using (System.Drawing.Graphics g = Graphics.FromImage(offScreenBmp)) {
|
|
|
97 |
g.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
98 |
// Draw the control
|
|
|
99 |
drawControl(g, this.On);
|
|
|
100 |
// Draw the image to the screen
|
|
|
101 |
e.Graphics.DrawImageUnscaled(offScreenBmp, 0, 0);
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/// <summary>
|
|
|
106 |
/// Renders the control to an image
|
|
|
107 |
/// </summary>
|
|
|
108 |
private void drawControl(Graphics g, bool on) {
|
|
|
109 |
// Is the bulb on or off
|
|
|
110 |
Color lightColor = (on)? this.Color : Color.FromArgb(150, this.DarkColor);
|
|
|
111 |
Color darkColor = (on) ? this.DarkColor : this.DarkDarkColor;
|
|
|
112 |
|
|
|
113 |
// Calculate the dimensions of the bulb
|
|
|
114 |
int width = this.Width - (this.Padding.Left + this.Padding.Right);
|
|
|
115 |
int height = this.Height - (this.Padding.Top + this.Padding.Bottom);
|
|
|
116 |
// Diameter is the lesser of width and height
|
|
|
117 |
int diameter = Math.Min(width, height);
|
|
|
118 |
// Subtract 1 pixel so ellipse doesn't get cut off
|
|
|
119 |
diameter = Math.Max(diameter - 1, 1);
|
|
|
120 |
|
|
|
121 |
// Draw the background ellipse
|
|
|
122 |
var rectangle = new Rectangle(this.Padding.Left, this.Padding.Top, diameter, diameter);
|
|
|
123 |
g.FillEllipse(new SolidBrush(darkColor), rectangle);
|
|
|
124 |
|
|
|
125 |
// Draw the glow gradient
|
|
|
126 |
var path = new GraphicsPath();
|
|
|
127 |
path.AddEllipse(rectangle);
|
|
|
128 |
var pathBrush = new PathGradientBrush(path);
|
|
|
129 |
pathBrush.CenterColor = lightColor;
|
|
|
130 |
pathBrush.SurroundColors = new Color[] { Color.FromArgb(0, lightColor) };
|
|
|
131 |
g.FillEllipse(pathBrush, rectangle);
|
|
|
132 |
|
|
|
133 |
// Draw the white reflection gradient
|
|
|
134 |
var offset = Convert.ToInt32(diameter * .15F);
|
|
|
135 |
var diameter1 = Convert.ToInt32(rectangle.Width * .8F);
|
|
|
136 |
var whiteRect = new Rectangle(rectangle.X - offset, rectangle.Y - offset, diameter1, diameter1);
|
|
|
137 |
var path1 = new GraphicsPath();
|
|
|
138 |
path1.AddEllipse(whiteRect);
|
|
|
139 |
var pathBrush1 = new PathGradientBrush(path);
|
|
|
140 |
pathBrush1.CenterColor = _reflectionColor;
|
|
|
141 |
pathBrush1.SurroundColors = _surroundColor;
|
|
|
142 |
g.FillEllipse(pathBrush1, whiteRect);
|
|
|
143 |
|
|
|
144 |
// Draw the border
|
|
|
145 |
g.SetClip(this.ClientRectangle);
|
|
|
146 |
if (this.On) g.DrawEllipse(new Pen(Color.FromArgb(85, Color.Black),1F), rectangle);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/// <summary>
|
|
|
150 |
/// Causes the Led to start blinking
|
|
|
151 |
/// </summary>
|
|
|
152 |
/// <param name="milliseconds">Number of milliseconds to blink for. 0 stops blinking</param>
|
|
|
153 |
public void Blink(int milliseconds) {
|
|
|
154 |
if (milliseconds > 0) {
|
|
|
155 |
this.On = true;
|
|
|
156 |
_timer.Interval = milliseconds;
|
|
|
157 |
_timer.Enabled = true;
|
|
|
158 |
}
|
|
|
159 |
else {
|
|
|
160 |
_timer.Enabled = false;
|
|
|
161 |
this.On = false;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
#endregion
|
|
|
166 |
}
|
|
|
167 |
}
|