C# 自定义控件:LED指示灯
下面我将介绍如何创建一个自定义的LED指示灯控件,它可以显示不同颜色(红、黄、绿等)和状态(亮、灭、闪烁),适用于工业监控、状态指示等场景。
1. 创建基础LED控件
1.1 创建自定义控件类
csharp
using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
public class LedIndicator : Control { // 字段 private Color ledColor = Color.Red; private bool ledState = false; private bool blinking = false; private int blinkInterval = 500; // 闪烁间隔(毫秒) private Timer blinkTimer; private int ledSize = 20; private bool threeState = false; private Color offColor = Color.DarkGray;
// 构造函数 public LedIndicator() { // 设置控件样式 SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
// 初始化定时器 blinkTimer = new Timer(); blinkTimer.Interval = blinkInterval; blinkTimer.Tick += BlinkTimer_Tick;
// 默认大小 this.Size = new Size(24, 24); }
// 属性 [Category("LED属性")] [Description("获取或设置LED颜色")] public Color LedColor { get { return ledColor; } set { ledColor = value; Invalidate(); } }
[Category("LED属性")] [Description("获取或设置LED状态")] public bool LedState { get { return ledState; } set { ledState = value; Invalidate(); } }
[Category("LED属性")] [Description("获取或设置是否闪烁")] public bool Blinking { get { return blinking; } set { blinking = value; blinkTimer.Enabled = blinking && ledState; Invalidate(); } }
[Category("LED属性")] [Description("获取或设置闪烁间隔(毫秒)")] public int BlinkInterval { get { return blinkInterval; } set { blinkInterval = Math.Max(50, value); // 最小50ms blinkTimer.Interval = blinkInterval; } }
[Category("LED属性")] [Description("获取或设置LED大小")] public int LedSize { get { return ledSize; } set { ledSize = Math.Max(10, value); Invalidate(); } }
[Category("LED属性")] [Description("启用三态模式(亮/灭/闪烁)")] public bool ThreeState { get { return threeState; } set { threeState = value; } }
[Category("LED属性")] [Description("LED关闭时的颜色")] public Color OffColor { get { return offColor; } set { offColor = value; Invalidate(); } }
// 方法 public void SetState(bool state) { LedState = state; if (blinking) { blinkTimer.Enabled = state; } }
// 事件处理 private void BlinkTimer_Tick(object sender, EventArgs e) { ledState = !ledState; Invalidate(); }
// 重写OnPaint方法 protected override void OnPaint(PaintEventArgs e) { base.onPaint(e);
Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias;
// 计算LED位置和大小 int diameter = Math.Min(ledSize, Math.Min(Width, Height) - 2); int x = (Width - diameter) / 2; int y = (Height - diameter) / 2; Rectangle rect = new Rectangle(x, y, diameter, diameter);
// 绘制LED using (GraphicsPath path = new GraphicsPath()) { path.AddEllipse(rect);
// 绘制LED主体 Color drawColor = (ledState || threeState) ? ledColor : offColor; using (PathGradientBrush pgb = new PathGradientBrush(path)) { pgb.CenterPoint = new PointF(rect.Left + rect.Width / 3, rect.Top + rect.Height / 3); pgb.CenterColor = Color.FromArgb(200, drawColor); pgb.SurroundColors = new Color[] { drawColor };
g.FillEllipse(pgb, rect); }
// 绘制高光 using (GraphicsPath highlightPath = new GraphicsPath()) { highlightPath.AddEllipse(rect.X + rect.Width / 4, rect.Y + rect.Height / 4, rect.Width / 2, rect.Height / 2); using (SolidBrush highlightBrush = new SolidBrush(Color.FromArgb(128, Color.White))) { g.FillEllipse(highlightBrush, rect.X + rect.Width / 4, rect.Y + rect.Height / 4, rect.Width / 2, rect.Height / 2); } }
// 绘制边框 using (Pen pen = new Pen(Color.DarkGray, 1f)) { g.DrawEllipse(pen, rect); } } }
// 重写大小相关方法 protected override void OnResize(EventArgs e) { base.onResize(e); // 保持控件为正方形 int size = Math.Min(Width, Height); this.Size = new Size(size, size); }
// 清理资源 protected override void Dispose(bool disposing) { if (disposing) { if (blinkTimer != null) { blinkTimer.Stop(); blinkTimer.Dispose(); } } base.Dispose(disposing); } }