问题描述
我知道如何使用默认的按钮项目,但有什么办法可以实现多行按钮的风格(或者更确切地说,点击文字?),如下所示?
的情况是,我有一个用户界面,选择什么样的文件,他希望建立,并且必须有下文字较大,主线的简要说明。
我只打算在Windows 7上运行这一点,所以我并不需要担心向下兼容旧版本的Windows
在截屏中显示的按钮实际上是1整个航空UI使用。这是按钮被称为命令链接的自定义样式,它可以很容易地应用到标准按钮
控制。
不幸的是,的WinForms库不暴露通过一个简单的属性这一功能,但是这很容易可以解决的有位P / Invoke的了。
您正在寻找的风格被称为 BS_COMMANDLINK
。据的文档,这种风格:
下面是一个扩展标准的WinForms 按钮
控制一个小自定义按钮控件类,并实现了命令链接的风格,你可以在设计或通过配置属性code。
这几件事情需要注意的code:
-
在
的FlatStyle
属性必须始终设置为FlatStyle.System
,这将强制使用标准Windows的API Button控件,而不是由的WinForms code绘制。这是必需的BS_COMMANDLINK
风格的工作(因为它只能由本地控制支持),并可以产生更好的寻找按钮控制(带悸动效果等)反正。要强制这一点,我已经覆盖了的FlatStyle
属性,并设置一个默认值。 -
在
CommandLink
属性是如何打开和关闭命令链接的风格。这是默认关闭的,给你一个标准的按键控制,这样你就可以在你的应用程序与这一个替换的所有的按钮控制的,如果你想,只是为了方便。当您打开属性上(将其设置为真
),那么你会得到一个花哨的,多行命令链接按钮。 -
命令链接按钮的标题是相同标题的显示在一个标准的按钮。然而,标题按钮的也的支持,第二行是说明。这是通过配置其他属性,名为
CommandLinkNote
,在WinAPI的消息之后,BCM_SETNOTE
。当您配置为标准按钮控制按钮(CommandLink =假
),该属性的值将被忽略。
进口System.Windows.Forms的进口System.ComponentModel进口了System.Runtime.InteropServices公共类ButtonEx:继承按钮 私人_commandLink为布尔 私人_commandLinkNote作为字符串 公共子新():MyBase.New() 在基类设置默认属性值,以避免过时的警告 MyBase.FlatStyle = FlatStyle.System 结束小组 <类别(外观)> _ <默认值(假)> _ <说明(指定此按钮应该使用命令链接的风格。&放大器; _ (仅适用于在Windows Vista和更高版本))> _ 公共属性CommandLink由于布尔 得到 返回_commandLink 最终获取 设置(BYVAL值作为布尔) 如果_commandLink<>值。然后 _commandLink =价值 Me.UpdateCommandLink() 结束如果 结束设定 高端物业 <类别(外观)> _ &其中;默认值()> _ <描述(设置的说明文字为一个命令链接按钮。&放大器; _ (仅适用于在Windows Vista和更高版本))> _ 公共属性CommandLinkNote作为字符串 得到 返回_commandLinkNote 最终获取 设置(值作为字符串) 如果_commandLinkNote<>值。然后 _commandLinkNote =价值 Me.UpdateCommandLink() 结束如果 结束设定 高端物业 <可浏览(假)> < EditorBrowsable(EditorBrowsableState.Never)> _ < DebuggerBrowsable(DebuggerBrowsableState.Never)> _ <过时(这家酒店是不支持ButtonEx控制。)> _ <默认值(的GetType(的FlatStyle),系统)> _ 公共阴影物业的FlatStyle作为的FlatStyle 设置默认扁平式的系统,并隐藏这个属性,因为 没有自定义属性将工作,没有将其设置为系统 得到 返回MyBase.FlatStyle 最终获取 设置(BYVAL值作为的FlatStyle) MyBase.FlatStyle =价值 结束设定 高端物业#REGIONP / Invoke的东西 私人常量BS_COMMANDLINK作为整数=安培;何 私人常量BCM_SETNOTE作为整数=安培; H1609 <的DllImport(user32.dll中,字符集:= CharSet.Uni code,SetLastError:= FALSE)> _ 私人共享SendMessage函数(BYVAL的HWND作为IntPtr的,BYVAL味精作为整数,BYVAL wParam中的IntPtr,_ <的MarshalAs(UnmanagedType.LPWStr)> lParam的BYVAL作为字符串)作为IntPtr的 端功能 私人小组UpdateCommandLink() Me.RecreateHandle() SendMessage函数(Me.Handle,BCM_SETNOTE,IntPtr.Zero,_commandLinkNote) 结束小组 受保护的覆盖只读属性的CreateParams作为的CreateParams 得到 昏暗的CP作为的CreateParams = MyBase.CreateParams 如果Me.CommandLink然后 cp.Style = cp.Style或者BS_COMMANDLINK 结束如果 返回CP 最终获取 高端物业#END地区末级
I know how to use the default buttons item, but is there any way to achieve the style of multiline buttons (or maybe rather, "clickable text"?) like shown below?
The situation is that I have an interface for the user to select what kind of file he wishes to establish, and there has to be a brief description under the larger, main line of text.
I'm only planning to run this on Windows 7, so I don't need to worry about backwards compatibility with older versions of Windows
The button shown in the screenshot is actually one used throughout the Aero UI. It's a custom style of button called a "command link", and it can be easily applied to a standard Button
control.
Unfortunately, the WinForms libraries don't expose this functionality via a simple property, but that's easily fixable with a bit of P/Invoke.
The style you're looking for is called BS_COMMANDLINK
. According to the documentation, this style:
Here's a little custom button control class that extends the standard WinForms Button
control, and implements the "command link" style as a property you can configure in the designer or through code.
A couple of things to note about the code:
The
FlatStyle
property must always be set toFlatStyle.System
, which forces the use of the standard Windows API Button control, rather than one drawn by WinForms code. This is required for theBS_COMMANDLINK
style to work (because it's only supported by the native controls), and it produces a better looking button control (with throbbing effects, etc.) anyway. To force this, I've overridden theFlatStyle
property and set a default value.The
CommandLink
property is how you toggle on and off the "command link" style. It's off by default, giving you a standard button control, so you can replace all of the button controls in your application with this one, if you want, just for convenience. When you turn the property on (set it toTrue
), then you get a fancy, multiline command link button.The command link button's caption is the same caption as is displayed on a standard button. However, caption button also support a "description" on the second line. This is configurable through another property, called
CommandLinkNote
, after the WinAPI message,BCM_SETNOTE
. When you have the button configured as a standard button control (CommandLink = False
), the value of this property is ignored.
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
这篇关于如何创建命令链接按钮(多行文本)在VB.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!