// 绘制高光 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); } } } else { // 方形高光 Rectangle highlightRect = new Rectangle( rect.X + rect.Width / 4, rect.Y + rect.Height / 4, rect.Width / 2, rect.Height / 4);
using (LinearGradientBrush lgb = new LinearGradientBrush( highlightRect, Color.FromArgb(180, Color.White), Color.FromArgb(30, Color.White), LinearGradientMode.Vertical)) { g.FillRectangle(lgb, highlightRect); } } }
// 重写大小相关方法 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) { blinkTimer?.Stop(); blinkTimer?.Dispose(); } base.Dispose(disposing); } }
// 扩展方法:为GraphicsPath添加圆角矩形 public static class GraphicsPathExtensions { public static void AddRoundedRectangle(this GraphicsPath path, Rectangle rect, int radius) { int diameter = radius * 2; Size size = new Size(diameter, diameter); Rectangle arc = new Rectangle(rect.Location, size);
// 左上角 path.AddArc(arc, 180, 90);
// 右上角 arc.X = rect.Right - diameter; path.AddArc(arc, 270, 90);
// 右下角 arc.Y = rect.Bottom - diameter; path.AddArc(arc, 0, 90);
// 左下角 arc.X = rect.Left; path.AddArc(arc, 90, 90);
path.CloseFigure(); } }
使用示例
基本使用
csharp
// 创建LED控件 LedIndicator led1 = new LedIndicator(); led1.LedColor = Color.Green; led1.LedState = true; led1.Location = new Point(20, 20); this.Controls.Add(led1);
// 创建闪烁LED LedIndicator led2 = new LedIndicator(); led2.LedColor = Color.Red; led2.Blinking = true; led2.BlinkInterval = 300; led2.Location = new Point(60, 20); this.Controls.Add(led2);
增强版LED使用
csharp
// 创建高级LED AdvancedLed advLed = new AdvancedLed(); advLed.LedColor = Color.Blue; advLed.LedShape = LedShape.RoundedSquare; advLed.LedStyle = LedStyle.ThreeD; advLed.CornerRadius = 8; advLed.LedState = true; advLed.Location = new Point(100, 20); this.Controls.Add(advLed);
// 通过按钮控制 Button toggleBtn = new Button(); toggleBtn.Text = "切换LED"; toggleBtn.Location = new Point(20, 60); toggleBtn.Click += (s, e) => advLed.ToggleState(); this.Controls.Add(toggleBtn);
设计时支持
为了使控件在设计时更易用,可以添加设计时特性:
csharp
// 在控件类中添加以下代码
[Designer(typeof(LedIndicatorDesigner))] public class LedIndicator : Control { // ... 之前的代码 ... }
// 创建自定义设计器 internal class LedIndicatorDesigner : ControlDesigner { public override SelectionRules SelectionRules { get { // 禁止控件在设计时调整大小 return base.SelectionRules & ~(SelectionRules.BottomSizeable | SelectionRules.TopSizeable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable); } } }
最佳实践
性能优化:
使用双缓冲(已通过ControlStyles.OptimizedDoubleBuffer启用)
避免在OnPaint中创建过多临时对象
可访问性:
实现Accessibility支持
添加适当的工具提示和键盘快捷键
主题支持:
可以添加对系统主题颜色的支持
实现IThemeable接口(如果需要)
序列化:
确保所有可设计属性都正确标记为[DesignerSerializationVisibility]
这个自定义LED控件提供了丰富的自定义选项,包括颜色、形状、样式、闪烁控制等,可以满足大多数工业监控和状态指示的需求。