Files
CheckDevice/Check.Main/UI/FormStatistics.cs
17860779768 2e46747ba9 视觉修改
2025-08-25 16:33:58 +08:00

90 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Check.Main.Camera;
using Check.Main.Common;
using Check.Main.Infer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Check.Main.UI
{
public partial class FormStatistics : DockContent
{
private readonly StatisticsData _statisticsData;
/// <summary>
/// 将私有字段通过公共属性暴露以便外部如FormMain访问。
/// </summary>
public StatisticsData CurrentStatistics => _statisticsData;
public FormStatistics()
{
InitializeComponent();
_statisticsData = new StatisticsData();
// 订阅CameraManager的检测完成事件
DetectionCoordinator.OnDetectionCompleted += CameraManager_OnDetectionCompleted;
// 初始化UI显示
UpdateUI();
}
// 当CameraManager发布检测结果时此方法被调用
private void CameraManager_OnDetectionCompleted(object sender, DetectionResultEventArgs e)
{
// 更新统计数据
_statisticsData.UpdateWithResult(e.IsOK);
// 在UI线程上更新界面显示
UpdateUI();
}
// 更新所有标签的文本
private void UpdateUI()
{
// 使用Invoke确保线程安全
if (this.InvokeRequired)
{
this.Invoke(new Action(UpdateUI));
return;
}
txtOKNum.Text = _statisticsData.GoodCount.ToString();
txtNGNum.Text = _statisticsData.NgCount.ToString();
txtTotal.Text = _statisticsData.TotalCount.ToString();
// 将良率格式化为百分比
txtYieldRate.Text = _statisticsData.YieldRate.ToString("P2", CultureInfo.InvariantCulture);
// 根据良率改变颜色以示提醒
if (_statisticsData.YieldRate < 0.9 && _statisticsData.TotalCount > 10)
{
txtYieldRate.ForeColor = Color.Red;
}
else
{
txtYieldRate.ForeColor = Color.ForestGreen;
}
}
private void toolStripButtonRest_Click(object sender, EventArgs e)
{
StatisticsExporter.ExportToExcel(CurrentStatistics, "Reset");
_statisticsData.Reset();
// 同时重置产品ID计数器
DetectionCoordinator.ResetAllCounters();
UpdateUI();
ThreadSafeLogger.Log("统计数据已重置。");
}
// 窗体关闭时,取消事件订阅,防止内存泄漏
protected override void OnFormClosing(FormClosingEventArgs e)
{
DetectionCoordinator.OnDetectionCompleted -= CameraManager_OnDetectionCompleted;
base.OnFormClosing(e);
}
}
}