[英]How do I create command link buttons (with multiple lines of text) in VB.NET?
我知道如何使用默认按钮项,但有没有办法实现多行按钮的风格(或者更确切地说,“可点击文本”?),如下所示?
情况是我有一个界面供用户选择他希望建立的文件类型,并且必须在较大的主要文本行下进行简要描述。
我只打算在Windows 7上运行它,所以我不需要担心与旧版Windows的向后兼容性
屏幕截图中显示的按钮实际上是整个Aero UI中使用的按钮。 它是一种称为“命令链接”的自定义按钮样式,可以轻松应用于标准的Button
控件。
不幸的是,WinForms库没有通过一个简单的属性公开这个功能,但是可以通过一些P / Invoke轻松修复。
您正在寻找的风格称为BS_COMMANDLINK
。 根据文档 ,这种风格:
创建一个命令链接按钮,其行为类似于
BS_PUSHBUTTON
样式按钮,但命令链接按钮左侧有一个绿色箭头指向按钮文本。 可以通过向按钮发送BCM_SETNOTE
消息来设置按钮文本的标题。
这是一个小的自定义按钮控件类,它扩展了标准的WinForms Button
控件,并将“命令链接”样式实现为您可以在设计器中或通过代码配置的属性。
有关代码的几点注意事项:
FlatStyle
属性必须始终设置为FlatStyle.System
,它强制使用标准Windows API Button控件,而不是WinForms代码绘制的控件。 这是BS_COMMANDLINK
样式工作所必需的(因为它仅由本机控件支持),并且无论如何它都会产生更好看的按钮控件(具有跳动效果等)。 为了强制这个,我重写了FlatStyle
属性并设置了一个默认值。
CommandLink
属性是您打开和关闭“命令链接”样式的方式。 它默认关闭,为您提供标准按钮控件,因此您可以使用此按钮替换应用程序中的所有按钮控件,只是为了方便起见。 当您打开属性(将其设置为True
)时,您将获得一个奇特的多行命令链接按钮。
命令链接按钮的标题与标准按钮上显示的标题相同。 但是,标题按钮也支持第二行的“描述”。 这可以通过WinAPI消息BCM_SETNOTE
之后的另一个属性(称为CommandLinkNote
)进行BCM_SETNOTE
。 如果将按钮配置为标准按钮控件( CommandLink = False
),则忽略此属性的值。
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class ButtonEx : Inherits Button
Private _commandLink As Boolean
Private _commandLinkNote As String
Public Sub New() : MyBase.New()
'Set default property values on the base class to avoid the Obsolete warning
MyBase.FlatStyle = FlatStyle.System
End Sub
<Category("Appearance")> _
<DefaultValue(False)> _
<Description("Specifies this button should use the command link style. " & _
"(Only applies under Windows Vista and later.)")> _
Public Property CommandLink As Boolean
Get
Return _commandLink
End Get
Set(ByVal value As Boolean)
If _commandLink <> value Then
_commandLink = value
Me.UpdateCommandLink()
End If
End Set
End Property
<Category("Appearance")> _
<DefaultValue("")> _
<Description("Sets the description text for a command link button. " & _
"(Only applies under Windows Vista and later.)")> _
Public Property CommandLinkNote As String
Get
Return _commandLinkNote
End Get
Set(value As String)
If _commandLinkNote <> value Then
_commandLinkNote = value
Me.UpdateCommandLink()
End If
End Set
End Property
<Browsable(False)> <EditorBrowsable(EditorBrowsableState.Never)> _
<DebuggerBrowsable(DebuggerBrowsableState.Never)> _
<Obsolete("This property is not supported on the ButtonEx control.")> _
<DefaultValue(GetType(FlatStyle), "System")> _
Public Shadows Property FlatStyle As FlatStyle
'Set the default flat style to "System", and hide this property because
'none of the custom properties will work without it set to "System"
Get
Return MyBase.FlatStyle
End Get
Set(ByVal value As FlatStyle)
MyBase.FlatStyle = value
End Set
End Property
#Region "P/Invoke Stuff"
Private Const BS_COMMANDLINK As Integer = &HE
Private Const BCM_SETNOTE As Integer = &H1609
<DllImport("user32.dll", CharSet:=CharSet.Unicode, SetLastError:=False)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As IntPtr
End Function
Private Sub UpdateCommandLink()
Me.RecreateHandle()
SendMessage(Me.Handle, BCM_SETNOTE, IntPtr.Zero, _commandLinkNote)
End Sub
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
If Me.CommandLink Then
cp.Style = cp.Style Or BS_COMMANDLINK
End If
Return cp
End Get
End Property
#End Region
End Class
不幸的是,接受的答案有错误,使用我的库,它是VistaUIFramework
,它包含一个更好的CommandLink。
CommandLink
CommandLinkNote
无效(如果您第二次运行您的应用程序) CommandLinkNote
不是多行的 CommandLink
控件 Note
总是有效(属性是Note
而不是CommandLinkNote
) Note
是多行的 VistaUIFramework还有另一个控件:
Hint
属性,一个替换空值的灰色文本) CloseBox
属性,它的工作方式类似于MaximizeBox
和MinimizeBox
,但它可以使用关闭按钮) State
属性来更改进度条的状态/颜色而不改变已安装操作系统的本机视觉样式) https://www.github.com/myapkapp/VistaUIFramework/
重要提示:这不是垃圾邮件,我只是假装发布一个提供更好的CommandLink和更多内容的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.