From 01a83fe4ffb24ca4e894d701247bb503cff4c588 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 21 Feb 2024 21:50:08 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIContextMenuStrip:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=BF=AB=E6=8D=B7=E9=94=AE=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UIContextMenuStrip.cs | 44 +++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/SunnyUI/Controls/UIContextMenuStrip.cs b/SunnyUI/Controls/UIContextMenuStrip.cs index defc2cec..aa446010 100644 --- a/SunnyUI/Controls/UIContextMenuStrip.cs +++ b/SunnyUI/Controls/UIContextMenuStrip.cs @@ -21,6 +21,7 @@ * 2022-03-19: V3.1.1 重构主题配色 * 2023-10-17: V3.5.1 修正文字显示垂直居中 * 2023-10-17: V3.5.1 当右键菜单未绑定ImageList,并且ImageIndex>0时,将ImageIndex绑定为Symbol绘制 + * 2024-02-21: V3.6.3 修复显示快捷键文本位置 ******************************************************************************/ using System.ComponentModel; @@ -36,7 +37,7 @@ namespace Sunny.UI public UIContextMenuStrip() { Font = UIStyles.Font(); - RenderMode = ToolStripRenderMode.Professional; + //RenderMode = ToolStripRenderMode.Custom; Renderer = new UIToolStripRenderer(ColorTable); Version = UIGlobal.Version; @@ -162,11 +163,26 @@ namespace Sunny.UI //调整文本区域的位置和大小以实现垂直居中 Rectangle textRect = new Rectangle(e.TextRectangle.Left, e.Item.ContentRectangle.Top, e.TextRectangle.Width, e.Item.ContentRectangle.Height); - //设置文本绘制格式 - TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine; + ToolStripMenuItem item1 = (ToolStripMenuItem)e.Item; - //绘制文本 - TextRenderer.DrawText(e.Graphics, e.Text, e.TextFont, textRect, e.TextColor, flags); + if (e.Item.Selected) + { + e.Graphics.FillRectangle(ColorTable.MenuItemSelected, e.TextRectangle); + } + else + { + e.Graphics.FillRectangle(ColorTable.ImageMarginGradientBegin, e.TextRectangle); + } + + //设置文本绘制格式 + TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.Left; + TextRenderer.DrawText(e.Graphics, e.Item.Text, e.TextFont, textRect, e.TextColor, flags); + + if (item1.ShowShortcutKeys) + { + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.Right; + TextRenderer.DrawText(e.Graphics, ShortcutToText(item1.ShortcutKeys, item1.ShortcutKeyDisplayString), e.TextFont, textRect, e.TextColor, flags); + } //当右键菜单未绑定ImageList,并且ImageIndex>0时,将ImageIndex绑定为Symbol绘制 ToolStripItem item = e.Item; @@ -183,6 +199,24 @@ namespace Sunny.UI Rectangle imageRect = new Rectangle(0, e.Item.ContentRectangle.Top, e.TextRectangle.Left, e.Item.ContentRectangle.Height); e.Graphics.DrawFontImage(e.Item.ImageIndex, 24, e.TextColor, imageRect); } + + internal static string ShortcutToText(Keys shortcutKeys, string shortcutKeyDisplayString) + { + if (!string.IsNullOrEmpty(shortcutKeyDisplayString)) + { + return shortcutKeyDisplayString; + } + + if (shortcutKeys == Keys.None) + { + return string.Empty; + } + + //KeysConverter kc = new KeysConverter(); + //kc.ConvertToString(item1.ShortcutKeys).WriteConsole(); + + return TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(shortcutKeys); + } } internal class ContextMenuColorTable : ProfessionalColorTable