简体   繁体   English

在 Winforms .NET Core 中使用命令链接

[英]Using Command Links in Winforms .NET Core

I would like to add command links into my C# Windows Forms app for Windows 10 21H2, but I haven't found any tutorials online.我想将命令链接添加到我的 C# Windows Forms 应用程序 Windows 10 21H2,但我没有在网上找到任何教程。

I believe that this is a photo of a command link, but I'm not too sure.我相信这是一张命令链接的照片,但我不太确定。 Can someone tell me how to add these to my app?有人可以告诉我如何将这些添加到我的应用程序中吗?

命令链接

I couldn't find any controls like this in the VS toolbox.我在 VS 工具箱中找不到这样的控件。

Try this Custom Control, derived from Button, where the BS_COMMANDLINK Style is added in the CreateParams property override.试试这个派生自 Button 的自定义控件,其中在 CreateParams 属性覆盖中添加了BS_COMMANDLINK样式。
This turns the standard Button Control in a CommandLink Button.这将在 CommandLink 按钮中转换标准按钮控件。
The Text Property value of the Control is shown to the right of the arrow, while the custom LinkNote Property provides the description that is shown right below the text, with a smaller Font.控件的Text属性值显示在箭头右侧,而自定义LinkNote属性提供显示在文本正下方的说明,字体较小。
When you set the LinkNote value, the BCM_SETNOTE message is sent to the native Button.当您设置LinkNote值时, BCM_SETNOTE消息将发送到本机 Button。 This sets the associated Note, at design-time or run-time.这会在设计时或运行时设置关联的注释。

The custom ControlDesigner is used to remove properties that are not needed / used when this style is set, or properties, as FlatStyle (which needs to be set to FlatStyle.System ), that cannot be changed.自定义 ControlDesigner 用于删除设置此样式时不需要/不使用的属性,或无法更改的属性,如FlatStyle (需要设置为FlatStyle.System )。
Unfortunately, this Designer works only in .NET Framework, to make it work in .NET 5+, you need to install the Microsoft.WinForms.Designer.SDK package. Open up the Package Manager Console and paste:不幸的是,此设计器仅适用于 .NET Framework,要使其适用于 .NET 5+,您需要安装Microsoft.WinForms.Designer.SDK package。打开 Package Manager Console 并粘贴:

NuGet\Install-Package Microsoft.WinForms.Designer.SDK -Version 1.1.0-prerelease-preview3.22076.5

This package is hard to find otherwise.否则很难找到这个 package。
If you don't care about the Designer's features, just remove it如果您不关心设计器的功能,只需将其删除

using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
#if NET5_0_OR_GREATER
    using Microsoft.DotNet.DesignTools.Designers;
#else
    using System.Windows.Forms.Design;
#endif

[ToolboxItem(true)]
[Designer(typeof(CommandLinkDesigner))]
public class CommandLink : Button {
    private const int BCM_SETNOTE = 0x1609;
    private string m_LinkNote = string.Empty;

    public CommandLink() {
        SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UseTextForAccessibility, true);
        FlatStyle = FlatStyle.System;
    }

    [DefaultValue(FlatStyle.System)]
    public new FlatStyle FlatStyle {
        get { return base.FlatStyle; }
        set { base.FlatStyle = FlatStyle.System; }
    }

    public string LinkNote {
        get => m_LinkNote;
        set {
            if (value != m_LinkNote) {
                m_LinkNote = value;
                SendMessage(this.Handle, BCM_SETNOTE, 0, m_LinkNote);
            }
        }
    }

    protected override CreateParams CreateParams {
        get {
            var cp = base.CreateParams;
            cp.Style |= (0 | 0x0E); // Set BS_COMMANDLINK 
            return cp;
        }
    }

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern bool SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

    public class CommandLinkDesigner : ControlDesigner {
        private readonly string[] RemovedProperties = new[] {
            "AllowDrop", "AutoEllipsis", "AutoSize", "AutoSizeMode",
            "BackColor", "BackgroundImage", "BackgroundImageLayout",
            "Cursor", "FlatAppearance", "FlatStyle",
            "ForeColor", "Image", "ImageAlign", "ImageImdex", "ImageKey",
            "ImageList", "TextAlign", "TextImageRelation"
        };

        public CommandLinkDesigner() { }

        protected override void PreFilterProperties(IDictionary properties)
        {
            foreach (string prop in RemovedProperties) {
                properties.Remove(prop);
            }
            base.PreFilterProperties(properties);
        }
    }
}

To add a command link to a Windows Forms application, you can use the CommandLink control from the System.Windows.Forms.VisualStyles namespace.要将命令链接添加到 Windows Forms 应用程序,您可以使用 System.Windows.Forms.VisualStyles 命名空间中的 CommandLink 控件。 Here is an example of how to use the CommandLink control in a Windows Forms application:以下是如何在 Windows Forms 应用程序中使用 CommandLink 控件的示例:

using System.Windows.Forms.VisualStyles;

// ...

// Create a new CommandLink control
CommandLink commandLink = new CommandLink();

// Set the text and description of the CommandLink
commandLink.Text = "Run the app";
commandLink.Description = "Click this button to run the app";

// Add the CommandLink to the form
this.Controls.Add(commandLink);

You can customize the appearance and behavior of the CommandLink control by setting its various properties, such as Text, Description, and UseVisualStyleBackColor.您可以通过设置其各种属性(例如 Text、Description 和 UseVisualStyleBackColor)来自定义 CommandLink 控件的外观和行为。 You can also handle the Click event of the CommandLink control to specify the action that should be taken when the user clicks the button.您还可以处理 CommandLink 控件的 Click 事件,以指定当用户单击按钮时应执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM