From 7ff8e1336374d6951c147edc31e57f9f4829288e Mon Sep 17 00:00:00 2001 From: Sunny Date: Thu, 22 Feb 2024 23:09:26 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIButton:=20=E5=A2=9E=E5=8A=A0=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E7=9A=84&=E5=AD=97=E7=AC=A6=E7=9A=84Alt=E5=BF=AB?= =?UTF-8?q?=E6=8D=B7=E9=94=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UIButton.cs | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/SunnyUI/Controls/UIButton.cs b/SunnyUI/Controls/UIButton.cs index 2e53573b..321b9cf5 100644 --- a/SunnyUI/Controls/UIButton.cs +++ b/SunnyUI/Controls/UIButton.cs @@ -31,6 +31,7 @@ * 2023-07-02: V3.3.9 渐变色增加方向选择 * 2023-11-24: V3.6.2 修复LightStyle的文字颜色 * 2023-12-06: V3.6.2 修复LightStyle的背景颜色 + * 2024-02-22: V3.6.3 增加按钮的&字符的Alt快捷键功能 ******************************************************************************/ using System; @@ -38,6 +39,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; +using System.Text; using System.Windows.Forms; namespace Sunny.UI @@ -75,6 +77,58 @@ namespace Sunny.UI fillPressColor = UIStyles.Blue.ButtonFillPressColor; fillSelectedColor = UIStyles.Blue.ButtonFillSelectedColor; SetStyle(ControlStyles.StandardDoubleClick, UseDoubleClick); + + UseMnemonic = true; + } + + [DefaultValue(true)] + [Description("如果为true,&符号后面的第一次字符将做按钮的助记键"), Category("SunnyUI")] + public bool UseMnemonic { get; set; } + + protected override bool ProcessMnemonic(char charCode) + { + if (UseMnemonic && CanProcessMnemonic() && IsMnemonic(charCode, Text)) + { + PerformClick(); + return true; + } + + return base.ProcessMnemonic(charCode); + } + + private bool CanProcessMnemonic() + { + return UseMnemonic && CanSelect && Enabled && Visible && Parent != null; + } + + public string TextWithoutMnemonics(string text) + { + if (text == null) + { + return null; + } + + int index = text.IndexOf('&'); + + if (index == -1) + { + return text; + } + + StringBuilder str = new StringBuilder(text.Substring(0, index)); + for (; index < text.Length; ++index) + { + if (text[index] == '&') + { + index++; // Skip this & and copy the next character instead + } + if (index < text.Length) + { + str.Append(text[index]); + } + } + + return str.ToString(); } ///