From 8dae174d25cd62218a30ff502c76dedab1c96b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E7=82=B9lilac?= Date: Fri, 13 Nov 2020 03:14:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BE=A7=E8=BE=B9=E6=A0=8F?= =?UTF-8?q?=E5=9B=BA=E5=AE=9A=E5=AE=BD=E5=BA=A6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BulePointLilac.Controls/MySideBar.cs | 23 +++++++++++++++---- ContextMenuManager/MainForm.cs | 11 +++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) 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