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; using AntdUI; namespace DHSoftware.Views { public partial class SettingWindow1 : Window { private UserControl currControl; public SettingWindow1() { InitializeComponent(); AntdUI.TooltipComponent tooltip = new AntdUI.TooltipComponent() { Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))), }; tooltip.ArrowAlign = AntdUI.TAlign.Right; tooltip.SetTip(btnAdd, "新增工位"); } private void btnAdd_Click(object sender, EventArgs e) { // 查找工位设置项 var workstationItem = FindMenuItem(menu1.Items, "工位设置"); if (workstationItem != null) { using (var dlg = new AddCubicleWindow()) { if (dlg.ShowDialog() == DialogResult.OK) { AddSubItem(workstationItem, dlg.CubicleName); } else { AntdUI.Notification.error(this, "操作失败", "未找到工位设置项", TAlignFrom.TR); } } } } // 递归查找菜单项 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; } // 添加子项方法 private void AddSubItem(MenuItem parentItem, string cubicleName) { // 创建带图标的菜单项 var newItem = new MenuItem(cubicleName) { IconSvg = "AppstoreOutlined", Tag = Guid.NewGuid() // 添加唯一标识 }; // 检查重复项(根据名称和ID) if (!parentItem.Sub.Cast().Any(m => m.Text == cubicleName && m.Tag?.ToString() == newItem.Tag.ToString())) { parentItem.Sub.Add(newItem); // 自动展开父项 if (!parentItem.Expand) parentItem.Expand = true; // 刷新菜单布局 menu1.Invalidate(); } else { AntdUI.Notification.warn(this, "添加失败", "工位名称已存在", TAlignFrom.TR); } } bool isUpdatingTabs = false; private void menu1_MouseDown(object sender, MouseEventArgs e) { // 转换坐标到控件内部坐标系(考虑滚动条) 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 "相机设置": 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 control = null; control = new DetectControl(this); if (control != null) { //容器添加控件,需要调整dpi control.Dock = DockStyle.Fill; AutoDpi(control); AntdUI.TabPage tabPage = new AntdUI.TabPage() { Text = $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}", }; tabPage.Controls.Add(control); tabs1.Pages.Add(tabPage); isUpdatingTabs = true; tabs1.SelectedTab = tabPage; isUpdatingTabs = false; currControl = control; } 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; } } }