Subversion Repositories group.electronics

Rev

Rev 142 | Rev 147 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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