在.NET Core WinForms中添加命令链接的方法求助
在.NET Core WinForms中实现Windows 10命令链接控件
你贴出的图确实是Windows原生的**命令链接(CommandLink)**控件,它属于Win32原生控件,.NET Core WinForms默认未将其加入VS工具箱,以下是两种可行的实现方式:
方法一:封装原生CommandLink控件(推荐)
通过P/Invoke调用Win32 API直接创建原生控件,能完美适配Windows 10的样式和交互逻辑:
- 创建自定义控件类:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class CommandLink : Button { private const int BCM_SETNOTE = 0x1609; private const int BS_COMMANDLINK = 0x0000000E; [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam); private string _noteText; public string NoteText { get => _noteText; set { _noteText = value; if (IsHandleCreated) SendMessage(Handle, BCM_SETNOTE, IntPtr.Zero, value); } } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style |= BS_COMMANDLINK; return cp; } } }
- 使用控件:
- 编译项目后,
CommandLink会自动出现在VS工具箱的自定义控件分组中,直接拖到窗体即可使用。 - 在属性面板设置
Text(主标题)和NoteText(下方说明文字),就能得到和示例图一致的效果。
方法二:用普通Button模拟(快速方案)
如果不想编写P/Invoke代码,可通过普通Button控件近似模拟样式:
- 设置Button的
FlatStyle为System - 配置
FlatAppearance中的MouseDownBackColor和MouseOverBackColor,模拟hover与点击的视觉反馈 - 用Label控件配合Button,或者在Button的
Text中加入换行符,实现主标题+说明文字的布局
内容的提问来源于stack exchange,提问作者X1525
相关产品推荐
相关产品推荐

