添加侧边栏固定宽度功能

This commit is contained in:
蓝点lilac
2020-11-13 03:14:09 +08:00
parent 1903effe1e
commit 8dae174d25
2 changed files with 29 additions and 5 deletions

View File

@@ -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,
};
/// <summary>获取项目宽度</summary>
public int GetItemWidth(string str)
{
return TextRenderer.MeasureText(str, Font).Width + 2 * HorizontalSpace;
}
/// <summary>绘制所有项目作为底图</summary>
private void PaintItems()
{
this.BackgroundImage = new Bitmap(Width, Height);
@@ -120,6 +129,7 @@ namespace BulePointLilac.Controls
}
}
/// <summary>刷新选中的项目</summary>
private void RefreshItem(Panel panel, int index)
{
panel.Top = index < 0 ? -ItemHeight : (TopSpace + index * ItemHeight);
@@ -127,14 +137,17 @@ namespace BulePointLilac.Controls
panel.Refresh();
}
/// <summary>绘制选中的项目</summary>
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));
}
/// <summary>显示选中的项目</summary>
private void ShowItem(Panel panel, MouseEventArgs e)
{
if(itemNames == null) return;

View File

@@ -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;
}
}
}