using System.ComponentModel; using System.Data; using System.Reflection; using AntdUI; using DH.Commons.Base; using DH.Commons.Enums; using DH.Devices.PLC; using XKRS.CanFly; using static System.Windows.Forms.AxHost; using static DH.Commons.Enums.EnumHelper; namespace DHSoftware.Views { public partial class SizeControl : UserControl { Window window; DetectionConfig detectionConfig; public SizeControl(Window _window,DetectionConfig _detection) { window = _window; detectionConfig = _detection; InitializeComponent(); //初始化表格列头 InitTableColumns(); InitData(); BindEventHandler(); } private void BindEventHandler() { btnSizeAdd.Click += BtnSizeAdd_Click; btnSizeDel.Click += BtnSizeDelete_Click; SizeTable.CellButtonClick += SizeTable_CellButtonClick; btnCorrelatedCamera.Click += BtnCorrelatedCamera_Click; } private void BtnCorrelatedCamera_Click(object? sender, EventArgs e) { var form = new CorrelatedCameraEdit(window, detectionConfig.CameraCollects) { Size = new Size(500, 400) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { flowCameraPanel.Controls.Clear(); if (detectionConfig.CameraCollects.Count > 0) { foreach (var item in detectionConfig.CameraCollects) { var control = new AntdUI.Tag() { Font = new System.Drawing.Font("Microsoft YaHei UI", 9F), Size = new Size(90, 42), Text = item.CameraSourceId, CloseIcon = true }; control.CloseChanged += (sender, e) => { var tag = sender as Tag; foreach (var item in detectionConfig.CameraCollects) { if (item.CameraSourceId.Equals(tag.Text)) { detectionConfig.CameraCollects.Remove(item); break; } } return true; }; // 通过主窗口设置DPI控制添加控件保持缩放比例 window.AutoDpi(control); flowCameraPanel.Controls.Add(control); control.BringToFront(); } } } } private void SizeTable_CellButtonClick(object sender, TableButtonEventArgs e) { var buttontext = e.Btn.Text; if (e.Record is SizeTreatParam sizeTreat) { switch (buttontext) { //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据 case "编辑": var form = new SizeLabelEdit(window, sizeTreat) { Size = new Size(500, 300) }; AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form) { OnLoad = () => { AntdUI.Message.info(window, "进入编辑", autoClose: 1); }, OnClose = () => { AntdUI.Message.info(window, "结束编辑", autoClose: 1); } }); break; case "删除": var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) detectionConfig.SizeTreatParamList.Remove(sizeTreat); break; case "进行测量": var sizeType = ((int)sizeTreat.PreType).ToString(); // 根据测量类型打开不同的窗口 switch (sizeType) { case "1": case "2": case "3": case "4": case "5": FrmMainSize frmMain3 = new FrmMainSize(sizeType, detectionConfig); frmMain3.ShowDialog(); if (!string.IsNullOrEmpty(frmMain3.inputtext)) { sizeTreat.ResultShow = frmMain3.inputtext; } if (!string.IsNullOrEmpty(frmMain3.outtext)) { sizeTreat.OutResultShow = frmMain3.outtext; } break; default: MessageBox.Show("未定义的测量类型!"); break; } //使用clone可以防止table中的image被修改 //Preview.open(new Preview.Config(window, (Image)SizeParamLable.CellImages[0].Image.Clone())); break; } } } private void BtnSizeDelete_Click(object? sender, EventArgs e) { if (detectionConfig.SizeTreatParamList.Count == 0 || !detectionConfig.SizeTreatParamList.Any(x => x.Selected)) { AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3); return; } var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) { // 使用反转for循环删除主列表中选中的项 for (int i = detectionConfig.SizeTreatParamList.Count - 1; i >= 0; i--) { // 删除选中的主列表项 if (detectionConfig.SizeTreatParamList[i].Selected) { detectionConfig.SizeTreatParamList.RemoveAt(i); } } // 提示删除完成 AntdUI.Message.success(window, "删除成功!", autoClose: 3); } } private void BtnSizeAdd_Click(object? sender, EventArgs e) { SizeTreatParam SizeParamLable = new SizeTreatParam() { //CellBadge = new CellBadge(SizeEnum.Circle.GetEnumDescription()), CellLinks = new CellLink[] { new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary), new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error), new CellButton(Guid.NewGuid().ToString(),"进行测量",TTypeMini.Primary) } }; var form = new SizeLabelEdit(window, SizeParamLable) { Size = new Size(450, 500) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { detectionConfig.SizeTreatParamList.Add(SizeParamLable); } } private void InitData() { SizeTable.Binding(detectionConfig.SizeTreatParamList); if (detectionConfig.SizeTreatParamList.Count > 0) { foreach (var item in detectionConfig.SizeTreatParamList) { item.CellLinks = new CellLink[] { new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) , new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error), new CellButton(Guid.NewGuid().ToString(),"进行测量",TTypeMini.Primary) }; } } } private void InitTableColumns() { SizeTable.Columns = new ColumnCollection() { new ColumnCheck("Selected"){Fixed = true}, new ColumnSwitch("IsEnable", "是否启用", ColumnAlign.Center), new Column("PreName", "测量名称",ColumnAlign.Center), new Column("PreType", "测量类型", ColumnAlign.Center), new Column("PrePix", "阈值", ColumnAlign.Center), new Column("ResultShow", "输入参数", ColumnAlign.Center), new Column("OutResultShow", "输出参数", ColumnAlign.Center), new Column("CellLinks", "操作", ColumnAlign.Center) }; } } }