From ad71cfb6bde2a2c005006f5cf871febfb664bab2 Mon Sep 17 00:00:00 2001 From: zqm Date: Wed, 12 Nov 2025 11:03:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AA=97=E5=8F=A3=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=8C=96=E5=92=8C=E8=BF=98=E5=8E=9F=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E6=80=A7=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=8A=82=E6=B5=81?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E5=92=8C=E7=BC=93=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Toprie/Toprie/Setting.Designer.cs | 2 +- .../CS/Framework4.0/Toprie/Toprie/Setting.cs | 160 ++++++++++++++---- .../Framework4.0/Toprie/Toprie/Setting.resx | 16 ++ .../Framework4.0/Toprie/Toprie/Toprie.csproj | 2 +- 4 files changed, 143 insertions(+), 37 deletions(-) diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs index 64d9f80..a62c702 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.Designer.cs @@ -146,7 +146,7 @@ namespace JoyD.Windows.CS // toolStrip // this.toolStrip.AllowDrop = true; - this.toolStrip.CanOverflow = false; + this.toolStrip.CanOverflow = true; this.toolStrip.Dock = System.Windows.Forms.DockStyle.None; this.toolStrip.ImageScalingSize = new System.Drawing.Size(20, 20); this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs index 989eecd..3f1f8ab 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs @@ -4108,14 +4108,26 @@ namespace JoyD.Windows.CS } } + // 上次调整时间,用于节流 + private DateTime lastAdjustTime = DateTime.MinValue; + // 节流时间间隔(毫秒) + private const int throttleInterval = 100; + // 缓存的可见按钮数量,避免每次都重新计算 + private int cachedVisibleButtonCount = -1; + /// /// 窗体大小变化事件处理程序 /// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸 /// private void Setting_Resize(object sender, EventArgs e) { - // 调用调整toolStrip尺寸的方法 - AdjustToolStripDimensions(); + // 实现节流,避免频繁调用 + DateTime now = DateTime.Now; + if ((now - lastAdjustTime).TotalMilliseconds >= throttleInterval) + { + AdjustToolStripDimensions(); + lastAdjustTime = now; + } } /// @@ -4125,43 +4137,23 @@ namespace JoyD.Windows.CS { try { - // 确保toolStripContainer和toolStrip不为null - if (toolStripContainer != null && toolStrip != null) + // 确保toolStripContainer和toolStrip不为null,并且窗体未被释放 + if (toolStripContainer != null && toolStrip != null && !this.IsDisposed) { - // 获取toolStripContainer的宽度 - int containerWidth = toolStripContainer.Width; - - // 设置toolStrip的宽度与container一致 - toolStrip.Width = containerWidth; - - // 计算可见按钮的数量 - int visibleButtonCount = 0; - foreach (ToolStripItem item in toolStrip.Items) + // 检查窗口句柄是否已创建,如果已创建则使用BeginInvoke,否则直接执行 + if (this.IsHandleCreated) { - if (item.Visible && !(item is ToolStripSeparator)) + // 使用BeginInvoke确保调整操作在UI线程的下一个消息循环中执行 + this.BeginInvoke((MethodInvoker)delegate { - visibleButtonCount++; - } + TryAdjustToolStripDimensions(); + }); + } + else + { + // 窗口句柄未创建时直接执行 + TryAdjustToolStripDimensions(); } - - // 根据可见按钮数量和容器宽度计算需要的行数 - int buttonWidth = 25; - int buttonsPerRow = Math.Max(1, containerWidth / buttonWidth); - int requiredRows = (int)Math.Ceiling((double)visibleButtonCount / buttonsPerRow); - - // 设置TopToolStripPanel的最小高度,确保有足够空间显示多行按钮 - int buttonHeight = 25; // 按钮高度加上边距 - int requiredHeight = requiredRows * buttonHeight + 10; // 加上额外边距 - - // 限制最大高度,防止高度过大 - requiredHeight = Math.Max(requiredHeight, 35); - toolStrip.MinimumSize = new Size(toolStrip.MinimumSize.Width, requiredHeight); - // 设置TopToolStripPanel的最小高度 - toolStripContainer.TopToolStripPanel.MinimumSize = new Size(toolStripContainer.TopToolStripPanel.MinimumSize.Width, requiredHeight); - - // 重新布局toolStrip和容器 - toolStrip.PerformLayout(); - toolStripContainer.PerformLayout(); } } catch (Exception ex) @@ -4169,6 +4161,104 @@ namespace JoyD.Windows.CS Console.WriteLine("调整toolStrip尺寸失败: " + ex.Message); } } + + /// + /// 实际执行toolStrip尺寸调整的逻辑 + /// + private void TryAdjustToolStripDimensions() + { + try + { + // 再次检查控件是否存在且不为null,并检查是否已被释放 + if (toolStripContainer != null && toolStrip != null && !this.IsDisposed && !toolStrip.IsDisposed && !toolStripContainer.IsDisposed) + { + // 获取toolStripContainer的宽度 + int containerWidth = toolStripContainer.Width; + + // 只有当宽度发生显著变化时才进行调整(容差5像素) + bool widthChanged = Math.Abs(toolStrip.Width - containerWidth) > 5; + if (widthChanged) + { + toolStrip.Width = containerWidth; + } + + // 优化:仅当缓存无效或按钮状态可能变化时才重新计算可见按钮数量 + bool needRecalculateButtons = cachedVisibleButtonCount == -1; + if (!needRecalculateButtons) + { + // 快速检查:只检查前几个按钮作为可见性变化的判断依据 + int quickCheckCount = 0; + foreach (ToolStripItem item in toolStrip.Items) + { + if (quickCheckCount >= 5) break; // 只检查前5个按钮 + if (item.Visible && !(item is ToolStripSeparator)) + { + quickCheckCount++; + } + } + // 如果快速检查结果与缓存的差异超过一半,则重新计算 + needRecalculateButtons = quickCheckCount > cachedVisibleButtonCount / 2 || quickCheckCount < cachedVisibleButtonCount / 2; + } + + // 重新计算可见按钮数量(如果需要) + if (needRecalculateButtons) + { + int visibleButtonCount = 0; + foreach (ToolStripItem item in toolStrip.Items) + { + if (item.Visible && !(item is ToolStripSeparator)) + { + visibleButtonCount++; + } + } + cachedVisibleButtonCount = visibleButtonCount; + } + + // 简化计算逻辑 + int buttonWidth = 40; + int buttonsPerRow = Math.Max(1, containerWidth / buttonWidth); + int requiredRows = Math.Min(3, (int)Math.Ceiling((double)cachedVisibleButtonCount / buttonsPerRow)); + + // 简化高度计算,使用固定值避免多次计算 + int requiredHeight = requiredRows * 40; + requiredHeight = Math.Max(requiredHeight, 60); + + // 只有当高度需要显著变化时才更新(容差10像素) + bool heightChanged = Math.Abs(toolStrip.MinimumSize.Height - requiredHeight) > 10; + if (heightChanged) + { + toolStrip.MinimumSize = new Size(toolStrip.MinimumSize.Width, requiredHeight); + // 设置TopToolStripPanel的最小高度 + toolStripContainer.TopToolStripPanel.MinimumSize = new Size(toolStripContainer.TopToolStripPanel.MinimumSize.Width, requiredHeight); + } + + // 只在实际有尺寸变化时执行布局,并使用BeginInvoke延迟执行 + if (widthChanged || heightChanged) + { + this.BeginInvoke((MethodInvoker)delegate + { + try + { + // 再次检查控件状态 + if (!this.IsDisposed && !toolStrip.IsDisposed && !toolStripContainer.IsDisposed) + { + toolStrip.PerformLayout(); + toolStripContainer.PerformLayout(); + } + } + catch (Exception innerEx) + { + Console.WriteLine("执行布局失败: " + innerEx.Message); + } + }); + } + } + } + catch (Exception ex) + { + Console.WriteLine("执行toolStrip尺寸调整失败: " + ex.Message); + } + } /// /// 根据颜色查找对应的温度值 /// diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.resx b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.resx index 2cc47af..1156463 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.resx +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.resx @@ -120,4 +120,20 @@ 516, 17 + + + + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 + YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAIFSURBVDhPpZLtS1NhGMbPPxJmmlYSgqHiKzGU1EDxg4iK + YKyG2WBogqMYJQOtCEVRFBGdTBCJfRnkS4VaaWNT5sqx1BUxRXxDHYxAJLvkusEeBaPAB+5z4Jzn+t3X + /aLhnEfjo8m+dCoa+7/C3O2Hqe0zDC+8KG+cRZHZhdzaaWTVTCLDMIY0vfM04Nfh77/G/sEhwpEDbO3t + I7TxE8urEVy99fT/AL5gWDLrTB/hnF4XsW0khCu5ln8DmJliT2AXrcNBsU1gj/MH4nMeKwBrPktM28xM + cX79DFKrHHD5d9D26hvicx4pABt2lpg10zYzU0zr7+e3xXGcrkEB2O2TNec9nJFwB3alZn5jZorfeDZh + 6Q3g8s06BeCoKF4MRURoH1+BY2oNCbeb0TIclIYxOhzf8frTOuo7FxCbbVIAzpni0iceEc8vhzEwGkJD + lx83ymxifejdKjRNk/8PWnyIyTQqAJek0jqHwfEVscu31baIu8+90sTE4nY025dQ2/5FIPpnXlzKuK8A + HBUzHot52djqQ6HZhfR7IwK4mKpHtvEDMqvfCiQ6zaAAXM8x94aIWTNrLLG4kVUzgaTSPlzLtyJOZxbb + 1wtfyg4Q+AfA3aZlButjSfxGcUJBk4g5tuP3haQKRKXcUQDOmbvNTpPOJeFFjordZmbWTNvMTHFUcpUC + nOccAdABIDXXE1nzAAAAAElFTkSuQmCC + + \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj b/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj index 800fcf0..821513c 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj @@ -1,4 +1,4 @@ - +