添加根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel尺寸的功能

This commit is contained in:
zqm
2025-11-10 14:54:44 +08:00
parent c8a49ec024
commit 393fa05d23

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@@ -63,6 +63,12 @@ namespace JoyD.Windows.CS
_timer = new Timer { Interval = 1000 }; _timer = new Timer { Interval = 1000 };
_timer.Tick += Timer_Tick; _timer.Tick += Timer_Tick;
// 添加窗体大小变化事件处理
this.Resize += new EventHandler(Setting_Resize);
// 初始调整一次toolStrip尺寸
AdjustToolStripDimensions();
// 初始隐藏颜色选择按钮 // 初始隐藏颜色选择按钮
btnSelectColor.Visible = false; btnSelectColor.Visible = false;
@@ -2255,5 +2261,68 @@ namespace JoyD.Windows.CS
catch {} catch {}
} }
} }
/// <summary>
/// 窗体大小变化事件处理程序
/// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸
/// </summary>
private void Setting_Resize(object sender, EventArgs e)
{
// 调用调整toolStrip尺寸的方法
AdjustToolStripDimensions();
}
/// <summary>
/// 根据toolStripContainer宽度动态调整toolStrip和TopToolStripPanel的尺寸
/// </summary>
private void AdjustToolStripDimensions()
{
try
{
// 确保toolStripContainer和toolStrip不为null
if (toolStripContainer != null && toolStrip != null)
{
// 获取toolStripContainer的宽度
int containerWidth = toolStripContainer.Width;
// 设置toolStrip的宽度与container一致
toolStrip.Width = containerWidth;
// 计算可见按钮的数量
int visibleButtonCount = 0;
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Visible && !(item is ToolStripSeparator))
{
visibleButtonCount++;
}
}
// 根据可见按钮数量和容器宽度计算需要的行数
// 假设每个按钮和分隔符大约占用60像素宽度
int buttonWidth = 60;
int buttonsPerRow = Math.Max(1, containerWidth / buttonWidth);
int requiredRows = (int)Math.Ceiling((double)visibleButtonCount / buttonsPerRow);
// 设置TopToolStripPanel的最小高度确保有足够空间显示多行按钮
int buttonHeight = 35; // 按钮高度加上边距
int requiredHeight = requiredRows * buttonHeight + 10; // 加上额外边距
// 限制最大高度,防止高度过大
requiredHeight = Math.Min(requiredHeight, 200);
// 设置TopToolStripPanel的最小高度
toolStripContainer.TopToolStripPanel.MinimumSize = new Size(toolStripContainer.TopToolStripPanel.MinimumSize.Width, requiredHeight);
// 重新布局toolStrip和容器
toolStrip.PerformLayout();
toolStripContainer.PerformLayout();
}
}
catch (Exception ex)
{
Console.WriteLine("调整toolStrip尺寸失败: " + ex.Message);
}
}
} }
} }