forked from ClassicDIY/ModbusTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedBulb.cs
More file actions
144 lines (118 loc) · 4.64 KB
/
LedBulb.cs
File metadata and controls
144 lines (118 loc) · 4.64 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Modbus.Common {
/// <summary>
/// The LEDBulb is a .Net control for Windows Forms that emulates an
/// LED light with two states On and Off. The purpose of the control is to
/// provide a sleek looking representation of an LED light that is sizable,
/// has a transparent background and can be set to different colors.
/// </summary>
public class LedBulb : Control {
#region Public and Private Members
private Color _color;
private bool _on = true;
/// <summary>
/// Gets or Sets the color of the LED light
/// </summary>
[DefaultValue(typeof(Color), "153, 255, 54")]
public Color Color {
get { return _color; }
set {
_color = value;
this.DarkColor = ControlPaint.Dark(_color);
this.DarkDarkColor = ControlPaint.DarkDark(_color);
this.Invalidate(); // Redraw the control
}
}
/// <summary>
/// Dark shade of the LED color used for gradient
/// </summary>
public Color DarkColor { get; protected set; }
/// <summary>
/// Very dark shade of the LED color used for gradient
/// </summary>
public Color DarkDarkColor { get; protected set; }
/// <summary>
/// Gets or Sets whether the light is turned on
/// </summary>
public bool On { get { return _on; } set { _on = value; this.Invalidate(); } }
#endregion
#region Constructor
public LedBulb() {
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Color = Color.FromArgb(255, 153, 255, 54);
}
#endregion
#region Transpanency Methods
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnMove(EventArgs e) {
RecreateHandle();
}
protected override void OnPaintBackground(PaintEventArgs e) {
}
#endregion
#region Drawing Methods
/// <summary>
/// Handles the Paint event for this UserControl
/// </summary>
protected override void OnPaint(PaintEventArgs e){
// Create an offscreen graphics object for double buffering
Bitmap offScreenBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
using (System.Drawing.Graphics g = Graphics.FromImage(offScreenBmp)) {
g.SmoothingMode = SmoothingMode.HighQuality;
// Draw the control
drawControl(g);
// Draw the image to the screen
e.Graphics.DrawImageUnscaled(offScreenBmp, 0, 0);
}
}
/// <summary>
/// Renders the control to an image
/// </summary>
/// <param name="g"></param>
private void drawControl(Graphics g) {
Color lightColor = (this.On)? this.Color : this.DarkColor;
Color darkColor = (this.On) ? this.DarkColor : this.DarkDarkColor;
Rectangle paddedRectangle = new Rectangle(this.Padding.Left, this.Padding.Top, this.Width - (this.Padding.Left + this.Padding.Right), this.Height - (this.Padding.Top + this.Padding.Bottom));
int width = (paddedRectangle.Width < paddedRectangle.Height) ? paddedRectangle.Width : paddedRectangle.Height;
Rectangle drawRectangle = new Rectangle(paddedRectangle.X, paddedRectangle.Y, width, width);
// Draw the background ellipse
if (drawRectangle.Width < 1) drawRectangle.Width = 1;
if (drawRectangle.Height < 1) drawRectangle.Height = 1;
g.FillEllipse(new SolidBrush(darkColor), drawRectangle);
// Draw the glow gradient
GraphicsPath path = new GraphicsPath();
path.AddEllipse(drawRectangle);
PathGradientBrush pathBrush = new PathGradientBrush(path);
pathBrush.CenterColor = lightColor;
pathBrush.SurroundColors = new Color[] { Color.FromArgb(0, lightColor) };
g.FillEllipse(pathBrush, drawRectangle);
// Set the clip boundary to the edge of the ellipse
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(drawRectangle);
g.SetClip(gp);
// Draw the white reflection gradient
GraphicsPath path1 = new GraphicsPath();
Rectangle whiteRect = new Rectangle(drawRectangle.X - Convert.ToInt32(drawRectangle.Width * .15F), drawRectangle.Y - Convert.ToInt32(drawRectangle.Width * .15F), Convert.ToInt32(drawRectangle.Width*.8F), Convert.ToInt32(drawRectangle.Height*.8F));
path1.AddEllipse(whiteRect);
PathGradientBrush pathBrush1 = new PathGradientBrush(path);
pathBrush1.CenterColor = Color.FromArgb(180, 255, 255, 255);
pathBrush1.SurroundColors = new Color[] { Color.FromArgb(0, 255, 255, 255) };
g.FillEllipse(pathBrush1, whiteRect);
// Draw the border
float w = drawRectangle.Width;
g.SetClip(this.ClientRectangle);
if (this.On) g.DrawEllipse(new Pen(Color.FromArgb(85, Color.Black),1F), drawRectangle);
}
#endregion
}
}