1.2 使用LED控件
将上述代码添加到你的项目中(可以创建一个单独的类文件)
编译项目后,LED控件会出现在工具箱中
拖拽到窗体上即可使用
2. 增强功能版LED控件
下面是一个增强版的LED控件,增加了更多特性:
csharp
using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
public enum LedShape { Circular, Square, RoundedSquare }
public enum LedStyle { Solid, Gradient, ThreeD }
public class AdvancedLed : 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 Color offColor = Color.DarkGray; private LedShape ledShape = LedShape.Circular; private LedStyle ledStyle = LedStyle.ThreeD; private int cornerRadius = 10; private bool showReflection = true;
// 构造函数 public AdvancedLed() { 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 => ledColor; set { ledColor = value; Invalidate(); } }
[Category("LED属性")] [Description("LED状态")] public bool LedState { get => ledState; set { ledState = value; UpdateBlinkState(); Invalidate(); } }
[Category("LED属性")] [Description("是否闪烁")] public bool Blinking { get => blinking; set { blinking = value; UpdateBlinkState(); Invalidate(); } }
[Category("LED属性")] [Description("闪烁间隔(毫秒)")] public int BlinkInterval { get => blinkInterval; set { blinkInterval = Math.Max(50, value); blinkTimer.Interval = blinkInterval; } }
[Category("LED属性")] [Description("LED大小")] public int LedSize { get => ledSize; set { ledSize = Math.Max(10, value); Invalidate(); } }
[Category("LED属性")] [Description("LED关闭时的颜色")] public Color OffColor { get => offColor; set { offColor = value; Invalidate(); } }
[Category("LED属性")] [Description("LED形状")] public LedShape LedShape { get => ledShape; set { ledShape = value; Invalidate(); } }
[Category("LED属性")] [Description("LED样式")] public LedStyle LedStyle { get => ledStyle; set { ledStyle = value; Invalidate(); } }
[Category("LED属性")] [Description("圆角半径(仅方形有效)")] public int CornerRadius { get => cornerRadius; set { cornerRadius = Math.Max(0, value); Invalidate(); } }
[Category("LED属性")] [Description("显示高光反射效果")] public bool ShowReflection { get => showReflection; set { showReflection = value; Invalidate(); } }
// 方法 public void ToggleState() { LedState = !LedState; }
private void UpdateBlinkState() { blinkTimer.Enabled = blinking && ledState; }
// 事件处理 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);
// 如果是方形,调整矩形 if (ledShape != LedShape.Circular) { rect = new Rectangle(x, y, diameter, diameter); }
// 绘制LED using (GraphicsPath path = CreateLedPath(rect)) { Color drawColor = ledState ? ledColor : offColor;
// 绘制背景 DrawLedBackground(g, path, drawColor);
// 绘制高光 if (showReflection) { DrawLedReflection(g, path, rect); }
// 绘制边框 using (Pen pen = new Pen(Color.FromArgb(150, Color.Black), 1f)) { g.DrawPath(pen, path); } } }
private GraphicsPath CreateLedPath(Rectangle rect) { GraphicsPath path = new GraphicsPath();
switch (ledShape) { case LedShape.Circular: path.AddEllipse(rect); break; case LedShape.Square: path.AddRectangle(rect); break; case LedShape.RoundedSquare: int radius = Math.Min(cornerRadius, rect.Width / 2); path.AddRoundedRectangle(rect, radius); break; }
return path; }
private void DrawLedBackground(Graphics g, GraphicsPath path, Color color) { switch (ledStyle) { case LedStyle.Solid: using (SolidBrush brush = new SolidBrush(color)) { g.FillPath(brush, path); } break;
case LedStyle.Gradient: using (PathGradientBrush pgb = new PathGradientBrush(path)) { pgb.CenterPoint = new PointF( path.GetBounds().Left + path.GetBounds().Width / 3, path.GetBounds().Top + path.GetBounds().Height / 3); pgb.CenterColor = Color.FromArgb(200, color); pgb.SurroundColors = new Color[] { color }; g.FillPath(pgb, path); } break;
case LedStyle.ThreeD: Rectangle bounds = Rectangle.Round(path.GetBounds()); using (LinearGradientBrush lgb = new LinearGradientBrush( bounds, Color.FromArgb(180, color), Color.FromArgb(80, color), LinearGradientMode.Vertical)) { g.FillPath(lgb, path); } break; } }
private void DrawLedReflection(Graphics g, GraphicsPath path, Rectangle rect) { if (ledShape == LedShape.Circular) { // 圆形高光 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.FillPath(highlightBrush, highlightPath); } } }