using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using System.Windows.Forms; using AntdUI; using AntdUI.Svg; using DH.Commons.Enums; using DH.Commons.Helper; using DH.Devices.Devices; using DH.Devices.PLC; using DHSoftware.Utils; namespace DHSoftware.Views { public partial class SettingWindow1 : Window { private UserControl currControl; List CameraBaseList = new List(); List PLCBaseList = new List(); List DetectionList = new List(); public SettingWindow1() { InitializeComponent(); BindEventHandler(); InitData(); } private void BindEventHandler() { Resize += SettingWindow1_Resize; btnAdd.SelectedValueChanged += btnAdd_SelectedValueChanged; btnSave.Click += BtnSave_Click; } private float x; //定义当前窗体的宽度 private float y; //定义当前窗体的高度 private void InitData() { btnAdd.Items.Clear(); btnAdd.Items.Add("相机设置"); btnAdd.Items.Add("工位设置"); btnAdd.Items.Add("运控设置"); x = Width; y = Height; AdaptiveHelper.setTag(this); // 从文件加载配置 var loadedConfig = ConfigManager.LoadConfig(); if (loadedConfig != null) { // 更新当前列表 CameraBaseList = loadedConfig.Cameras; PLCBaseList = loadedConfig.PLCs; DetectionList = loadedConfig.Detections; if (CameraBaseList.Count > 0) { var workstationItem = FindMenuItem(menu1.Items, "相机设置"); if (workstationItem != null) { foreach (var item in CameraBaseList) { var newItem = new MenuItem(); newItem.Text = item.CameraName; newItem.IconSvg = "VideoCameraOutlined"; workstationItem.Sub.Add(newItem); } } } if (DetectionList.Count > 0) { var workstationItem = FindMenuItem(menu1.Items, "工位设置"); if (workstationItem != null) { foreach (var item in DetectionList) { var newItem = new MenuItem(); newItem.Text = item.Name; newItem.IconSvg = "AppstoreOutlined"; workstationItem.Sub.Add(newItem); } } } if (PLCBaseList.Count > 0) { var workstationItem = FindMenuItem(menu1.Items, "运控设置"); if (workstationItem != null) { foreach (var item in PLCBaseList) { var newItem = new MenuItem(); newItem.Text = item.PLCName; newItem.IconSvg = "ControlOutlined"; workstationItem.Sub.Add(newItem); } } } } } private void BtnSave_Click(object? sender, EventArgs e) { var config = ConfigManager.CreateConfig(CameraBaseList, PLCBaseList, DetectionList); ConfigManager.SaveConfig(config); MessageBox.Show("保存成功!"); } private void SettingWindow1_Resize(object? sender, EventArgs e) { var newx = Width / x; var newy = Height / y; AdaptiveHelper.setControls(newx, newy, this); } // 递归查找菜单项 private MenuItem FindMenuItem(MenuItemCollection items, string targetText) { foreach (MenuItem item in items) { if (item.Text == targetText) return item; if (item.Sub != null) { var subResult = FindMenuItem(item.Sub, targetText); if (subResult != null) return subResult; } } return null; } bool isUpdatingTabs = false; private void menu1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { // 转换坐标到控件内部坐标系(考虑滚动条) Point clickPoint = new Point(e.X, e.Y + menu1.ScrollBar.Value); // 递归查找命中的菜单项 MenuItem clickedItem = FindClickedItem(menu1.Items, clickPoint); if (clickedItem != null) { if (clickedItem.PARENTITEM == null) { return; } switch (clickedItem.PARENTITEM.Text) { case "相机设置": foreach (var tab in tabs1.Pages) { if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}") { isUpdatingTabs = true; tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage isUpdatingTabs = false; currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null; return; } } //先获取是否存在该名称的配置 //如果没有新建项 CameraBase? CameraBase= CameraBaseList.Where(c => c.CameraName == clickedItem.Text).FirstOrDefault(); if (CameraBase == null) { CameraBase=new CameraBase(); } UserControl control = null; control = new CameraControl(this, CameraBase); if (control != null) { //容器添加控件,需要调整dpi control.Dock = DockStyle.Fill; AutoDpi(control); AntdUI.TabPage tabPage = new AntdUI.TabPage() { Text = $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}", ReadOnly = false, }; tabPage.Controls.Add(control); tabs1.Pages.Add(tabPage); isUpdatingTabs = true; tabs1.SelectedTab = tabPage; isUpdatingTabs = false; currControl = control; } break; case "工位设置": // 检查是否已存在同名 TabPage foreach (var tab in tabs1.Pages) { if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}") { isUpdatingTabs = true; tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage isUpdatingTabs = false; currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null; return; } } UserControl control1 = null; control1 = new DetectControl(this); if (control1 != null) { //容器添加控件,需要调整dpi control1.Dock = DockStyle.Fill; AutoDpi(control1); AntdUI.TabPage tabPage = new AntdUI.TabPage() { Text = $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}", ReadOnly = false , }; tabPage.Controls.Add(control1); tabs1.Pages.Add(tabPage); isUpdatingTabs = true; tabs1.SelectedTab = tabPage; isUpdatingTabs = false; currControl = control1; } break; case "运控设置": break; } } } else if (e.Button == MouseButtons.Right) { // 转换坐标到控件内部坐标系(考虑滚动条) Point clickPoint = new Point(e.X, e.Y + menu1.ScrollBar.Value); // 递归查找命中的菜单项 MenuItem clickedItem = FindClickedItem(menu1.Items, clickPoint); if (clickedItem != null) { if (clickedItem.PARENTITEM == null) { return; } switch (clickedItem.PARENTITEM.Text) { case "相机设置": var menulist = new AntdUI.IContextMenuStripItem[] { new AntdUI.ContextMenuStripItem("删除相机", "") { IconSvg = "DeleteOutlined" } }; AntdUI.ContextMenuStrip.open(menu1, it => { menu1.Remove(clickedItem); foreach (var tab in tabs1.Pages) { if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}") { isUpdatingTabs = true; tabs1.Pages.Remove(tab); CameraBaseList.RemoveAll(c => c.CameraName == clickedItem.Text); isUpdatingTabs = false; currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null; return; } } CameraBaseList.RemoveAll(c => c.CameraName == clickedItem.Text); }, menulist); break; case "工位设置": var menulist1 = new AntdUI.IContextMenuStripItem[] { new AntdUI.ContextMenuStripItem("删除工位", "") { IconSvg = "DeleteOutlined" } }; AntdUI.ContextMenuStrip.open(menu1, it => { menu1.Remove(clickedItem); foreach (var tab in tabs1.Pages) { if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}") { isUpdatingTabs = true; //tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage tabs1.Pages.Remove(tab); isUpdatingTabs = false; currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null; return; } } }, menulist1); break; case "运控设置": break; } } } } private MenuItem FindClickedItem(MenuItemCollection items, Point clickPoint) { foreach (MenuItem item in items) { // 检查当前项是否可见且包含点击坐标 if (item.Visible && item.Rect.Contains(clickPoint)) { return item; } // 递归检查子项(如果展开) if (item.Expand && item.Sub != null) { var childResult = FindClickedItem(item.Sub, clickPoint); if (childResult != null) return childResult; } } return null; } private void btnAdd_SelectedValueChanged(object sender, ObjectNEventArgs e) { string selectedValue = e.Value.ToString(); switch (selectedValue) { case "相机设置": var workstationItem = FindMenuItem(menu1.Items, "相机设置"); if (workstationItem != null) { var form = new AddCameraControl(this) { Size = new Size(300, 200) }; AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { var newItem = new MenuItem(form.CubicleName); newItem.IconSvg = "VideoCameraOutlined"; // 防止重复添加 if (!menu1.Items.Cast().Any(m => m.Text == newItem.Text)) { workstationItem.Sub.Add(newItem); CameraBase cameraBase = new CameraBase(); cameraBase.CameraName = form.CubicleName; CameraBaseList.Add(cameraBase); } else { AntdUI.Notification.warn(this, "新增相机失败", $"{form.CubicleName}已存在!", autoClose: 3, align: TAlignFrom.TR); } } } break; case "工位设置": // 查找工位设置项 var workstationItem1= FindMenuItem(menu1.Items, "工位设置"); if (workstationItem1 != null) { var form = new AddCubicleControl(this) { Size = new Size(300, 200) }; AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { var newItem = new MenuItem(form.CubicleName); newItem.IconSvg = "AppstoreOutlined"; // 防止重复添加 if (!menu1.Items.Cast().Any(m => m.Text == newItem.Text)) { workstationItem1.Sub.Add(newItem); } else { AntdUI.Notification.warn(this, "新增工位失败", $"{form.CubicleName}已存在!", autoClose: 3, align: TAlignFrom.TR); } } } break; case "运控设置": break; } } } }