Files

193 lines
6.4 KiB
C#
Raw Permalink Normal View History

2026-08-04 18:36:40 +08:00
using JinYuan.Models;
using SeeSharpTools.JY.GUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
namespace JinYuan.ControlLib
{
public enum BatteryStatus
{
Init, // 初始化-浅灰
PackNG, // 装箱NG-红色
Replacing, // 替换中-天蓝色
Normal // 正常-绿色
}
public class BatteryControl : LED
{
// 位置信息
public string Grade { get; set; } // 等级(对应Tab页)
public int Layer { get; set; } // 层
public int Row { get; set; } // 行
public int Col { get; set; } // 列
public string Container { get; set; }// 容器编号
public string Barcode { get; set; } // 电池条码
public BatteryStatus Status { get; set; } = BatteryStatus.Init;
private readonly Size _squareSize = new Size(25, 25);
public Size BatterySquareSize => _squareSize;
public BatteryControl(BatteryStatus status)
: this("", 0, 0, 0, "")
{
UpdateStatus(status);
}
// 构造函数:初始化工业风样式
public BatteryControl(string grade, int layer, int row, int col, string container, BatteryStatus status = BatteryStatus.Init)
{
// 基础位置信息
this.Grade = grade;
this.Layer = layer;
this.Row = row;
this.Col = col;
this.Container = container;
// 核心样式配置
this.Size = _squareSize;
this.MinimumSize = _squareSize;
this.MaximumSize = _squareSize;
this.BorderStyle = BorderStyle.None;
this.BackColor = Color.FromArgb(24, 24, 24);
this.Margin = new Padding(2);
// LED控件核心配置
this.Style = LedStyle.Circular;
this.Interacton = InteractionStyle.Indicator;
this.BlinkOn = false;
this.BlinkInterval = 500;
// 初始化LED基础颜色
this.OffColor = Color.Gray; // 默认关闭颜色(Init状态)
this.OnColor = Color.LimeGreen;// 默认开启颜色(Normal状态)
this.Value = false; // 默认关闭(Init状态)
// 双缓冲优化,防止闪烁
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
ResizeRedraw = false; // 禁止调整大小重绘
// 默认状态
UpdateStatus(status);
// 点击事件
Click += (s, e) => OnBatteryClicked?.Invoke(this);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
// 强制保持正方形
if (this.Size != _squareSize)
{
this.Size = _squareSize;
}
}
// 更新状态方法,确保颜色立即生效
public void UpdateStatus(BatteryStatus status)
{
// 防止跨线程调用问题
if (this.InvokeRequired)
{
this.Invoke(new Action<BatteryStatus>(UpdateStatus), status);
return;
}
this.Status = status;
switch (status)
{
case BatteryStatus.Normal:
this.Value = true; // 必须设为true才能显示OnColor
this.OnColor = Color.LimeGreen;
this.OffColor = Color.Transparent;
break;
case BatteryStatus.PackNG:
this.Value = true; // 必须设为true才能显示OnColor
this.OnColor = Color.Red;
this.OffColor = Color.Transparent;
break;
case BatteryStatus.Init:
this.Value = false; // 设为false显示OffColor
this.OffColor = Color.Gray;
this.OnColor = Color.Transparent;
this.Barcode = null;
break;
case BatteryStatus.Replacing:
this.Value = true; // 必须设为true才能显示OnColor
this.OnColor = Color.SkyBlue;
this.OffColor = Color.Transparent;
break;
}
// 强制刷新控件,确保颜色立即更新
this.Invalidate();
//this.Update();
//this.Refresh();
}
/// <summary>
/// 绑定数据(线程安全+状态更新)
/// </summary>
public void BindData(string barcode, BatteryStatus status, string container = null)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string, BatteryStatus, string>(BindData), barcode, status, container);
return;
}
Barcode = barcode;
if (!string.IsNullOrEmpty(container))
{
Container = container;
}
// 仅状态变化时更新
if (Status != status)
{
UpdateStatus(status);
}
}
/// <summary>
/// 清理控件(重置为初始状态)
/// </summary>
public void Clear()
{
BindData(null, BatteryStatus.Init, Container);
}
// 选中事件回调
public event Action<BatteryControl> OnBatteryClicked;
// 重写OnPaint方法,确保绘制生效
protected override void OnPaint(PaintEventArgs e)
{
// 启用双缓冲和高质量绘制
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
base.OnPaint(e);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// BatteryControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.Name = "BatteryControl";
this.Size = new System.Drawing.Size(40, 40);
this.ResumeLayout(false);
}
}
}