You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在.NET Core WinForms中添加命令链接的方法求助

在.NET Core WinForms中实现Windows 10命令链接控件

你贴出的图确实是Windows原生的**命令链接(CommandLink)**控件,它属于Win32原生控件,.NET Core WinForms默认未将其加入VS工具箱,以下是两种可行的实现方式:

方法一:封装原生CommandLink控件(推荐)

通过P/Invoke调用Win32 API直接创建原生控件,能完美适配Windows 10的样式和交互逻辑:

  1. 创建自定义控件类:
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;
        }
    }
}
  1. 使用控件:
  • 编译项目后,CommandLink会自动出现在VS工具箱的自定义控件分组中,直接拖到窗体即可使用。
  • 在属性面板设置Text(主标题)和NoteText(下方说明文字),就能得到和示例图一致的效果。

方法二:用普通Button模拟(快速方案)

如果不想编写P/Invoke代码,可通过普通Button控件近似模拟样式:

  • 设置Button的FlatStyle为System
  • 配置FlatAppearance中的MouseDownBackColor和MouseOverBackColor,模拟hover与点击的视觉反馈
  • 用Label控件配合Button,或者在Button的Text中加入换行符,实现主标题+说明文字的布局

内容的提问来源于stack exchange,提问作者X1525

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:15:57