diff --git a/ContextMenuManager/BulePointLilac.Controls/MySideBar.cs b/ContextMenuManager/BulePointLilac.Controls/MySideBar.cs
index 017d7d2..82fbe07 100644
--- a/ContextMenuManager/BulePointLilac.Controls/MySideBar.cs
+++ b/ContextMenuManager/BulePointLilac.Controls/MySideBar.cs
@@ -28,12 +28,13 @@ namespace BulePointLilac.Controls
set
{
itemNames = value;
- if(value != null)
+ if(value != null && !IsFixedWidth)
{
int maxWidth = 0;
- Array.ForEach(value, item => maxWidth = Math.Max(maxWidth, TextRenderer.MeasureText(item, Font).Width));
- PnlHovered.Width = PnlSelected.Width = Width = maxWidth + 2 * HorizontalSpace;
+ Array.ForEach(value, str => maxWidth = Math.Max(maxWidth, GetItemWidth(str)));
+ this.Width = maxWidth + 2 * HorizontalSpace;
}
+ PnlHovered.Width = PnlSelected.Width = this.Width;
PaintItems();
SelectIndex = -1;
}
@@ -49,6 +50,7 @@ namespace BulePointLilac.Controls
public int HorizontalSpace { get; set; } = 20.DpiZoom();//项文字与项左右边缘距离
private float VerticalSpace => (itemHeight - TextHeight) * 0.5F;//项文字与项上下边缘距离
private int TextHeight => TextRenderer.MeasureText(" ", Font).Height;//项文字高度
+ public bool IsFixedWidth { get; set; } = true;//是否固定宽度
public Color SeparatorColor
{
@@ -95,6 +97,13 @@ namespace BulePointLilac.Controls
Width = 1,
};
+ /// 获取项目宽度
+ public int GetItemWidth(string str)
+ {
+ return TextRenderer.MeasureText(str, Font).Width + 2 * HorizontalSpace;
+ }
+
+ /// 绘制所有项目作为底图
private void PaintItems()
{
this.BackgroundImage = new Bitmap(Width, Height);
@@ -120,6 +129,7 @@ namespace BulePointLilac.Controls
}
}
+ /// 刷新选中的项目
private void RefreshItem(Panel panel, int index)
{
panel.Top = index < 0 ? -ItemHeight : (TopSpace + index * ItemHeight);
@@ -127,14 +137,17 @@ namespace BulePointLilac.Controls
panel.Refresh();
}
+ /// 绘制选中的项目
private void PaintItem(object sender, PaintEventArgs e)
{
Control ctr = (Control)sender;
e.Graphics.Clear(ctr.BackColor);
e.Graphics.DrawString(ctr.Text, Font,
- new SolidBrush(ctr.ForeColor), new PointF(HorizontalSpace, VerticalSpace));
+ new SolidBrush(ctr.ForeColor),
+ new PointF(HorizontalSpace, VerticalSpace));
}
-
+
+ /// 显示选中的项目
private void ShowItem(Panel panel, MouseEventArgs e)
{
if(itemNames == null) return;
diff --git a/ContextMenuManager/MainForm.cs b/ContextMenuManager/MainForm.cs
index caaaeda..bc5a694 100644
--- a/ContextMenuManager/MainForm.cs
+++ b/ContextMenuManager/MainForm.cs
@@ -1,5 +1,7 @@
using BulePointLilac.Controls;
using ContextMenuManager.Controls;
+using System;
+using System.Linq;
namespace ContextMenuManager
{
@@ -7,6 +9,7 @@ namespace ContextMenuManager
{
public MainForm()
{
+ SetSideBarWidth();
this.Text = AppString.General.AppName;
this.Controls.Add(new ExplorerRestarter());
shellList.Owner = shellNewList.Owner = sendToList.Owner = openWithList.Owner
@@ -281,5 +284,13 @@ namespace ContextMenuManager
return;
}
}
+
+ private void SetSideBarWidth()
+ {
+ int maxWidth = 0;
+ string[] strs = GeneralItems.Concat(TypeItems).Concat(OtherRuleItems).Concat(AboutItems).ToArray();
+ Array.ForEach(strs, str => maxWidth = Math.Max(maxWidth, SideBar.GetItemWidth(str)));
+ SideBar.Width = maxWidth;
+ }
}
}
\ No newline at end of file