图标文字在缩放的时候修改为换行显示
This commit is contained in:
parent
8d3725f9be
commit
ae8c1b0c6a
46
MainForm.cs
46
MainForm.cs
@ -1176,21 +1176,39 @@ namespace QuickLauncher
|
||||
Cursor = Cursors.Hand
|
||||
};
|
||||
|
||||
var label = new Label
|
||||
{
|
||||
Text = item.Name,
|
||||
AutoSize = false,
|
||||
TextAlign = ContentAlignment.MiddleCenter,
|
||||
Width = panel.Width,
|
||||
Height = 30,
|
||||
Location = new Point(0, iconSize + 10),
|
||||
Tag = item,
|
||||
Cursor = Cursors.Hand,
|
||||
ForeColor = isDarkMode ? Color.FromArgb(240, 240, 240) : Color.FromArgb(20, 20, 20)
|
||||
};
|
||||
// 创建一个新的文本框控件实例
|
||||
var label = new TextBox();
|
||||
|
||||
panel.Controls.Add(icon);
|
||||
panel.Controls.Add(label);
|
||||
// 计算文本所需的高度
|
||||
// TextRenderer.MeasureText: 测量指定文本在给定条件下需要的尺寸
|
||||
// item.Name: 要显示的文本内容
|
||||
// new Font(): 创建微软雅黑8号字体
|
||||
// new Size(panel.Width, 0): 指定宽度限制,高度不限
|
||||
// TextFormatFlags.WordBreak: 允许在单词间换行
|
||||
// TextFormatFlags.TextBoxControl: 使用文本框的文本渲染规则
|
||||
// .Height + 10: 获取计算出的高度并额外添加10像素的边距
|
||||
var textHeight = TextRenderer.MeasureText(item.Name, new Font("Microsoft YaHei", 8), new Size(panel.Width, 0), TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl).Height+10;
|
||||
|
||||
label.Text = item.Name; // 设置显示的文本内容
|
||||
label.AutoSize = false; // 禁用自动尺寸调整
|
||||
label.TextAlign = HorizontalAlignment.Center; // 文本水平居中对齐
|
||||
label.MinimumSize = new Size(panel.Width, 30); // 设置最小尺寸
|
||||
label.MaximumSize = new Size(panelWidth, 0); // 设置最大宽度,高度不限
|
||||
label.Location = new Point(0, iconSize + 20); // 设置位置(在图标下方10像素)
|
||||
label.Tag = item; // 存储关联的数据项
|
||||
label.Cursor = Cursors.Hand; // 鼠标悬停时显示手型光标
|
||||
label.ForeColor = isDarkMode ? Color.FromArgb(240, 240, 240) : Color.FromArgb(20, 20, 20); // 根据暗/亮模式设置文字颜色
|
||||
label.Padding = new Padding(0); // 设置内边距为2像素
|
||||
label.Font = new Font("Microsoft YaHei", 8); // 设置字体为微软雅黑8号
|
||||
label.Multiline = true; // 启用多行显示
|
||||
label.ReadOnly = true; // 设置为只读,防止编辑
|
||||
label.BorderStyle = BorderStyle.None; // 移除边框
|
||||
label.BackColor = panel.BackColor; // 背景色与面板一致
|
||||
label.Height = textHeight; // 应用计算出的高度
|
||||
|
||||
// 将图标和文本框添加到面板中
|
||||
panel.Controls.Add(icon); // 添加图标控件
|
||||
panel.Controls.Add(label); // 添加文本框控件
|
||||
|
||||
// 设置右键菜单
|
||||
panel.ContextMenuStrip = shortcutItemContextMenu;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user