From 30c19f8679849e1375c1fc159524306939248961 Mon Sep 17 00:00:00 2001 From: czz_y Date: Thu, 16 Jan 2025 10:45:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DMenu=E5=AD=90=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=A6=82=E7=8E=87=E6=97=A0=E6=B3=95=E7=82=B9=E5=87=BB?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0Tabs=E7=82=B9=E5=87=BB=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E8=B7=9F=E8=B8=AAMenu=E9=80=89=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainWindow.Designer.cs | 13 +++++++------ MainWindow.cs | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/MainWindow.Designer.cs b/MainWindow.Designer.cs index d4dfd5b..8960ab5 100644 --- a/MainWindow.Designer.cs +++ b/MainWindow.Designer.cs @@ -130,10 +130,10 @@ this.tabs.Cursor = System.Windows.Forms.Cursors.Default; this.tabs.Dock = System.Windows.Forms.DockStyle.Fill; this.tabs.Gap = 20; - this.tabs.Location = new System.Drawing.Point(50, 40); + this.tabs.Location = new System.Drawing.Point(70, 40); this.tabs.Name = "tabs"; this.tabs.Pages.Add(this.tabPage); - this.tabs.Size = new System.Drawing.Size(974, 560); + this.tabs.Size = new System.Drawing.Size(954, 560); styleCard2.Closable = true; this.tabs.Style = styleCard2; this.tabs.TabIndex = 9; @@ -146,7 +146,7 @@ this.tabPage.Location = new System.Drawing.Point(3, 40); this.tabPage.Name = "tabPage"; this.tabPage.ReadOnly = true; - this.tabPage.Size = new System.Drawing.Size(968, 517); + this.tabPage.Size = new System.Drawing.Size(948, 517); this.tabPage.TabIndex = 1; this.tabPage.Text = "首页"; // @@ -157,7 +157,7 @@ this.panel_content.Location = new System.Drawing.Point(0, 0); this.panel_content.Name = "panel_content"; this.panel_content.Radius = 0; - this.panel_content.Size = new System.Drawing.Size(968, 517); + this.panel_content.Size = new System.Drawing.Size(948, 517); this.panel_content.TabIndex = 4; // // menu @@ -168,9 +168,10 @@ this.menu.IconRatio = 1F; this.menu.Indent = true; this.menu.Location = new System.Drawing.Point(0, 40); + this.menu.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); this.menu.Name = "menu"; - this.menu.Padding = new System.Windows.Forms.Padding(6); - this.menu.Size = new System.Drawing.Size(50, 560); + this.menu.Padding = new System.Windows.Forms.Padding(6, 6, 0, 6); + this.menu.Size = new System.Drawing.Size(70, 560); this.menu.TabIndex = 8; this.menu.Unique = true; // diff --git a/MainWindow.cs b/MainWindow.cs index 7e5b602..d711c58 100644 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -6,6 +6,7 @@ using AntdUIDemo.Views; using AntdUIDemo.Views.SubView; using Microsoft.Win32; using System; +using System.Linq; using System.Windows.Forms; namespace AntdUIDemo @@ -13,6 +14,7 @@ namespace AntdUIDemo public partial class MainWindow : AntdUI.Window { private UserControl currControl; + private bool isUpdatingTabs = false; public MainWindow() { InitializeComponent(); @@ -40,10 +42,42 @@ namespace AntdUIDemo input_search.TextChanged += Input_search_textchanged; menu.SelectChanged += Menu_SelectChanged; button_collapse.Click += button_collapse_Click; + tabs.SelectedIndexChanged += Tabs_SelectedIndexChanged; //监听系统深浅色变化 SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; } + private void Tabs_SelectedIndexChanged(object sender, IntEventArgs e) + { + if (isUpdatingTabs) return; + var text = tabs.SelectedTab?.Text; // 使用安全导航操作符,防止 SelectedTab 为 null + if (string.IsNullOrEmpty(text)) // 检查 text 是否为 null 或空 + { + return; // 如果 text 为空,直接退出方法 + } + //首页 + if (text == "首页") + { + return; + } + var rootIndex = 0; + var subIndex = 0; + var menuItemsCopy = menu.Items.ToList(); // 创建副本 + for (int i = 0; i < menuItemsCopy.Count; i++) + { + for (int j = 0; j < menuItemsCopy[i].Sub.Count; j++) + { + if (menuItemsCopy[i].Sub[j].Tag.ToString() == text) + { + rootIndex = i; + subIndex = j; + break; + } + } + } + menu.SelectIndex(rootIndex, subIndex, true); + } + private void button_collapse_Click(object sender, EventArgs e) { if (menu.Collapsed) @@ -52,7 +86,7 @@ namespace AntdUIDemo } else { - menu.Width = (int)(50 * Config.Dpi); + menu.Width = (int)(70 * Config.Dpi); } button_collapse.Toggle = !button_collapse.Toggle; menu.Collapsed = !menu.Collapsed; @@ -385,12 +419,15 @@ namespace AntdUIDemo //容器添加控件,需要调整dpi control.Dock = DockStyle.Fill; AutoDpi(control); - AntdUI.TabPage tabPage = new AntdUI.TabPage() { + AntdUI.TabPage tabPage = new AntdUI.TabPage() + { Text = name, }; tabPage.Controls.Add(control); tabs.Pages.Add(tabPage); + isUpdatingTabs = true; tabs.SelectedTab = tabPage; + isUpdatingTabs = false; currControl = control; } }