如何为C#项目的按钮控件文本设置多颜色以突出显示快捷键字符
C# 快捷键字符高亮多色按钮实现方案
以下是WinForm、WPF两种常用C#桌面框架的实现方案,无需引入第三方依赖:
一、WinForm 实现方案
核心思路是重写按钮的OnPaint绘制事件,将文本拆分为「普通前缀+快捷键字符+普通后缀」三部分,分别用不同颜色的画刷绘制,同时保留按钮原有交互样式。
自定义按钮代码
public class HotkeyButton : Button { // 要高亮的快捷键字符 public char HotkeyChar { get; set; } // 快捷键高亮色 public Color HotkeyColor { get; set; } = Color.Red; // 普通文本颜色 public Color NormalTextColor { get; set; } = SystemColors.ControlText; protected override void OnPaint(PaintEventArgs e) { // 绘制按钮基础背景、边框、点击状态 ButtonState state = Enabled ? (MouseButtons == MouseButtons.Left && ClientRectangle.Contains(PointToClient(Cursor.Position)) ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive; ButtonRenderer.DrawButton(e.Graphics, ClientRectangle, state); if (string.IsNullOrEmpty(Text)) return; // 匹配快捷键字符位置 int hotkeyIndex = Text.IndexOf(HotkeyChar.ToString(), StringComparison.OrdinalIgnoreCase); if (hotkeyIndex < 0) { // 未匹配到快捷键时正常绘制全文 using (Brush normalBrush = new SolidBrush(NormalTextColor)) { e.Graphics.DrawString(Text, Font, normalBrush, ClientRectangle, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }); } return; } // 拆分三段文本 string preText = Text.Substring(0, hotkeyIndex); string hotkeyStr = HotkeyChar.ToString(); string postText = Text.Substring(hotkeyIndex + 1); // 计算各段文本尺寸 SizeF preSize = e.Graphics.MeasureString(preText, Font); SizeF hotkeySize = e.Graphics.MeasureString(hotkeyStr, Font); SizeF postSize = e.Graphics.MeasureString(postText, Font); using (Brush normalBrush = new SolidBrush(NormalTextColor)) using (Brush hotkeyBrush = new SolidBrush(HotkeyColor)) { // 计算文本整体居中偏移量 float totalWidth = preSize.Width + hotkeySize.Width + postSize.Width; float offsetX = (ClientRectangle.Width - totalWidth) / 2; float offsetY = (ClientRectangle.Height - preSize.Height) / 2; // 分段绘制文本 e.Graphics.DrawString(preText, Font, normalBrush, offsetX, offsetY); e.Graphics.DrawString(hotkeyStr, Font, hotkeyBrush, offsetX + preSize.Width, offsetY); e.Graphics.DrawString(postText, Font, normalBrush, offsetX + preSize.Width + hotkeySize.Width, offsetY); } } }
使用方法
- 编译项目后,工具箱会自动出现
HotkeyButton控件,直接拖入窗体即可使用 - 在属性面板设置
HotkeyChar为需要高亮的快捷键字符,HotkeyColor为自定义高亮色即可生效
二、WPF 实现方案
WPF可直接通过TextBlock的内联元素实现多色文本,不需要重绘逻辑:
快速实现(无需自定义控件)
直接在XAML中拼接不同颜色的Run元素即可:
<Button Click="AddRecord_Click"> <TextBlock> <Run Text="新增(" /> <Run Text="F5" Foreground="Red" /> <Run Text=")" /> </TextBlock> </Button>
通用自定义控件实现
如果需要批量复用,可自定义带绑定属性的按钮控件:
public class HotkeyButton : Button { // 快捷键文本依赖属性 public static readonly DependencyProperty HotkeyTextProperty = DependencyProperty.Register("HotkeyText", typeof(string), typeof(HotkeyButton), new PropertyMetadata(string.Empty)); // 快捷键高亮色依赖属性 public static readonly DependencyProperty HotkeyForegroundProperty = DependencyProperty.Register("HotkeyForeground", typeof(Brush), typeof(HotkeyButton), new PropertyMetadata(Brushes.Red)); public string HotkeyText { get => (string)GetValue(HotkeyTextProperty); set => SetValue(HotkeyTextProperty, value); } public Brush HotkeyForeground { get => (Brush)GetValue(HotkeyForegroundProperty); set => SetValue(HotkeyForegroundProperty, value); } }
内容的提问来源于stack exchange,提问作者Ankit Goel
相关产品推荐
相关产品推荐

