using AntdUI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Controls { public enum LedStatus { Default, // 默认状态(灰色) Running, // 运行状态(绿色) Idle, // 闲置状态(橙色) Alarm // 报警状态(红色) } [ToolboxItem(true)] [Description("一个用于显示通信状态的LED指示器控件")] public partial class UCLedAvatar : UserControl { private Avatar avatar; private LedStatus _status = LedStatus.Default; private bool _isBlinking = false; private bool _blinkState = false; private System.Windows.Forms.Timer _blinkTimer; // 定义状态对应的颜色 private static readonly Dictionary StatusColors = new Dictionary { { LedStatus.Default, Color.FromArgb(128, 128, 128) }, // 灰色 { LedStatus.Running, Color.FromArgb(16, 192, 101) }, // 绿色 #10c065 { LedStatus.Idle, Color.FromArgb(255, 147, 0) }, // 橙色 #ff9300 { LedStatus.Alarm, Color.FromArgb(241, 11, 18) } // 红色 #f10b12 }; // SVG 模板 private const string SVG_TEMPLATE = @" "; public UCLedAvatar() { InitializeComponent(); InitializeAvatar(); SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.Transparent; } private void InitializeAvatar() { avatar = new Avatar { Dock = DockStyle.Fill, Round = false, ImageSvg = string.Format(SVG_TEMPLATE, ColorTranslator.ToHtml(StatusColors[LedStatus.Default])) }; this.Controls.Add(avatar); InitializeTimer(); } private void InitializeTimer() { _blinkTimer = new System.Windows.Forms.Timer(); _blinkTimer.Tick += BlinkTimer_Tick; _blinkTimer.Interval = 500; // 默认闪烁间隔 } [Category("行为")] [DefaultValue(LedStatus.Default)] public LedStatus Status { get => _status; set { if (_status != value) { _status = value; UpdateLedState(); } } } [Category("行为")] [DefaultValue(false)] public bool IsBlinking { get => _isBlinking; set { if (_isBlinking != value) { _isBlinking = value; if (_isBlinking) { _blinkTimer.Start(); } else { _blinkTimer.Stop(); _blinkState = false; UpdateLedState(); } } } } [Category("行为")] [DefaultValue(500)] public int BlinkInterval { get => _blinkTimer.Interval; set { if (value > 0 && _blinkTimer.Interval != value) { _blinkTimer.Interval = value; } } } private void BlinkTimer_Tick(object sender, EventArgs e) { _blinkState = !_blinkState; UpdateLedState(); } private void UpdateLedState() { Color currentColor; if (_status == LedStatus.Alarm && _isBlinking) { // 报警状态下的闪烁在灰色和红色之间切换 currentColor = _blinkState ? StatusColors[LedStatus.Alarm] : StatusColors[LedStatus.Default]; } else { // 非报警状态不闪烁 currentColor = StatusColors[_status]; } avatar.ImageSvg = string.Format(SVG_TEMPLATE, ColorTranslator.ToHtml(currentColor)); } } }