工具栏控件(ToolBar)
提供可自定义的工具栏,支持扁平/标准样式、下拉按钮、按钮菜单、用户自定义和OLE拖放。
枚举
TbrStyleConstants
工具栏样式常量。
| 常量 | 值 | 说明 |
|---|---|---|
| TbrStyleStandard | 0 | 标准工具栏样式 |
| TbrStyleFlat | 1 | 扁平工具栏样式 |
TbrTextAlignConstants
按钮文本对齐方式常量。
| 常量 | 值 | 说明 |
|---|---|---|
| TbrTextAlignBottom | 0 | 文本显示在按钮底部 |
| TbrTextAlignRight | 1 | 文本显示在按钮右侧 |
TbrOrientationConstants
工具栏方向常量。
| 常量 | 值 | 说明 |
|---|---|---|
| TbrOrientationHorizontal | 0 | 水平方向 |
| TbrOrientationVertical | 1 | 垂直方向 |
TbrButtonStyleConstants
按钮样式常量。
| 常量 | 值 | 说明 |
|---|---|---|
| TbrButtonDefault | 0 | 默认按钮样式 |
| TbrButtonCheck | 1 | 复选按钮样式 |
| TbrButtonCheckGroup | 2 | 复选组按钮样式(同组互斥) |
| TbrButtonSeparator | 3 | 分隔符 |
| TbrButtonDropDown | 4 | 下拉按钮样式 |
| TbrButtonWholeDropDown | 5 | 整体下拉按钮样式 |
TbrButtonValueConstants
按钮状态值常量。
| 常量 | 值 | 说明 |
|---|---|---|
| TbrButtonUnpressed | 0 | 未按下状态 |
| TbrButtonPressed | 1 | 按下状态 |
属性
Name
Public Property Get Name() As String返回在代码中标识对象的名称。
Tag
Public Property Get Tag() As String
Public Property Let Tag(ByVal Value As String)存储程序所需的额外数据。
Parent
Public Property Get Parent() As Object返回对象所在的对象。
Container
Public Property Get Container() As Object
Public Property Set Container(ByVal Value As Object)返回/设置对象的容器。
Left
Public Property Get Left() As Single
Public Property Let Left(ByVal Value As Single)返回/设置对象与其容器左边缘的距离。
Top
Public Property Get Top() As Single
Public Property Let Top(ByVal Value As Single)返回/设置对象与其容器顶边缘的距离。
Width
Public Property Get Width() As Single
Public Property Let Width(ByVal Value As Single)返回/设置对象的宽度。
Height
Public Property Get Height() As Single
Public Property Let Height(ByVal Value As Single)返回/设置对象的高度。
Visible
Public Property Get Visible() As Boolean
Public Property Let Visible(ByVal Value As Boolean)返回/设置对象是否可见。
ToolTipText
Public Property Get ToolTipText() As String
Public Property Let ToolTipText(ByVal Value As String)返回/设置鼠标悬停时显示的提示文本。
WhatsThisHelpID
Public Property Get WhatsThisHelpID() As Long
Public Property Let WhatsThisHelpID(ByVal Value As Long)返回/设置关联的上下文帮助ID。
Align
Public Property Get Align() As Integer
Public Property Let Align(ByVal Value As Integer)返回/设置控件在其窗体上的对齐方式。
DragIcon
Public Property Get DragIcon() As IPictureDisp
Public Property Let DragIcon(ByVal Value As IPictureDisp)
Public Property Set DragIcon(ByVal Value As IPictureDisp)返回/设置拖放操作中显示的图标。
DragMode
Public Property Get DragMode() As Integer
Public Property Let DragMode(ByVal Value As Integer)返回/设置拖动模式(手动或自动)。
hWnd
Public Property Get hWnd() As LongPtr返回控件句柄。
hWndUserControl
Public Property Get hWndUserControl() As LongPtr返回UserControl句柄。
Font
Public Property Get Font() As StdFont
Public Property Let Font(ByVal NewFont As StdFont)
Public Property Set Font(ByVal NewFont As StdFont)返回/设置字体。
VisualStyles
Public Property Get VisualStyles() As Boolean
Public Property Let VisualStyles(ByVal Value As Boolean)返回/设置是否启用视觉样式。需要comctl32.dll 6.0或更高版本。
Enabled
Public Property Get Enabled() As Boolean
Public Property Let Enabled(ByVal Value As Boolean)返回/设置对象是否能响应用户事件。
OLEDropMode
Public Property Get OLEDropMode() As OLEDropModeConstants
Public Property Let OLEDropMode(ByVal Value As OLEDropModeConstants)返回/设置对象是否可以作为OLE放置目标。
MousePointer
Public Property Get MousePointer() As CCMousePointerConstants
Public Property Let MousePointer(ByVal Value As CCMousePointerConstants)返回/设置鼠标悬停时显示的指针类型。参见通用枚举。
MouseIcon
Public Property Get MouseIcon() As IPictureDisp
Public Property Let MouseIcon(ByVal Value As IPictureDisp)
Public Property Set MouseIcon(ByVal Value As IPictureDisp)返回/设置自定义鼠标图标。
MouseTrack
Public Property Get MouseTrack() As Boolean
Public Property Let MouseTrack(ByVal Value As Boolean)返回/设置是否在鼠标进入或离开控件时触发事件。
RightToLeft
Public Property Get RightToLeft() As Boolean
Public Property Let RightToLeft(ByVal Value As Boolean)返回/设置从右到左显示方向。
RightToLeftLayout
Public Property Get RightToLeftLayout() As Boolean
Public Property Let RightToLeftLayout(ByVal Value As Boolean)返回/设置从右到左布局。
RightToLeftMode
Public Property Get RightToLeftMode() As CCRightToLeftModeConstants
Public Property Let RightToLeftMode(ByVal Value As CCRightToLeftModeConstants)返回/设置从右到左模式。参见通用枚举。
ImageList
Public Property Get ImageList() As Variant
Public Property Let ImageList(ByVal Value As Variant)
Public Property Set ImageList(ByVal Value As Variant)返回/设置关联的ImageList控件。可以是对象引用、字符串键名或LongPtr句柄。
DisabledImageList
Public Property Get DisabledImageList() As Variant
Public Property Let DisabledImageList(ByVal Value As Variant)
Public Property Set DisabledImageList(ByVal Value As Variant)返回/设置按钮禁用状态使用的ImageList控件。
HotImageList
Public Property Get HotImageList() As Variant
Public Property Let HotImageList(ByVal Value As Variant)
Public Property Set HotImageList(ByVal Value As Variant)返回/设置按钮热点状态使用的ImageList控件。
PressedImageList
Public Property Get PressedImageList() As Variant
Public Property Let PressedImageList(ByVal Value As Variant)
Public Property Set PressedImageList(ByVal Value As Variant)返回/设置按钮按下状态使用的ImageList控件。
BackColor
Public Property Get BackColor() As OLE_COLOR
Public Property Let BackColor(ByVal Value As OLE_COLOR)返回/设置背景色。
Style
Public Property Get Style() As TbrStyleConstants
Public Property Let Style(ByVal Value As TbrStyleConstants)返回/设置工具栏样式。
TextAlignment
Public Property Get TextAlignment() As TbrTextAlignConstants
Public Property Let TextAlignment(ByVal Value As TbrTextAlignConstants)返回/设置按钮文本对齐方式。
Orientation
Public Property Get Orientation() As TbrOrientationConstants
Public Property Let Orientation(ByVal Value As TbrOrientationConstants)返回/设置工具栏方向。
Divider
Public Property Get Divider() As Boolean
Public Property Let Divider(ByVal Value As Boolean)返回/设置是否显示分隔线。
ShowTips
Public Property Get ShowTips() As Boolean
Public Property Let ShowTips(ByVal Value As Boolean)返回/设置是否显示工具提示。
Wrappable
Public Property Get Wrappable() As Boolean
Public Property Let Wrappable(ByVal Value As Boolean)返回/设置按钮是否自动换行。
AllowCustomize
Public Property Get AllowCustomize() As Boolean
Public Property Let AllowCustomize(ByVal Value As Boolean)返回/设置是否允许用户自定义工具栏。
AltDrag
Public Property Get AltDrag() As Boolean
Public Property Let AltDrag(ByVal Value As Boolean)返回/设置是否允许Alt+拖动来自定义工具栏。
DoubleBuffer
Public Property Get DoubleBuffer() As Boolean
Public Property Let DoubleBuffer(ByVal Value As Boolean)返回/设置是否启用双缓冲绘制。
ButtonHeight
Public Property Get ButtonHeight() As Single
Public Property Let ButtonHeight(ByVal Value As Single)返回/设置按钮高度。
ButtonWidth
Public Property Get ButtonWidth() As Single
Public Property Let ButtonWidth(ByVal Value As Single)返回/设置按钮宽度。
MinButtonWidth
Public Property Get MinButtonWidth() As Single
Public Property Let MinButtonWidth(ByVal Value As Single)返回/设置最小按钮宽度。
MaxButtonWidth
Public Property Get MaxButtonWidth() As Single
Public Property Let MaxButtonWidth(ByVal Value As Single)返回/设置最大按钮宽度。
InsertMarkColor
Public Property Get InsertMarkColor() As OLE_COLOR
Public Property Let InsertMarkColor(ByVal Value As OLE_COLOR)返回/设置插入标记颜色。
Transparent
Public Property Get Transparent() As Boolean
Public Property Let Transparent(ByVal Value As Boolean)返回/设置工具栏是否透明。
HotTracking
Public Property Get HotTracking() As Boolean
Public Property Let HotTracking(ByVal Value As Boolean)返回/设置是否启用热点跟踪。
HideClippedButtons
Public Property Get HideClippedButtons() As Boolean
Public Property Let HideClippedButtons(ByVal Value As Boolean)返回/设置是否隐藏被裁剪的按钮。
AnchorHot
Public Property Get AnchorHot() As Boolean
Public Property Let AnchorHot(ByVal Value As Boolean)返回/设置是否锚定热点。
MaxTextRows
Public Property Get MaxTextRows() As Integer
Public Property Let MaxTextRows(ByVal Value As Integer)返回/设置最大文本行数。
Buttons
Public Property Get Buttons() As TbrButtons返回按钮集合。
方法
OLEDrag
Public Sub OLEDrag()启动OLE拖放操作。
Drag
Public Sub Drag(Optional ByRef Action As Variant)开始、结束或取消拖动操作。
ZOrder
Public Sub ZOrder(Optional ByRef Position As Variant)设置Z顺序。
Refresh
Public Sub Refresh()强制完全重绘对象。
事件
Click
Public Event Click()用户单击控件时触发。
DblClick
Public Event DblClick()用户双击控件时触发。
Resize
Public Event Resize()控件大小改变时触发。
BeginCustomization
Public Event BeginCustomization()开始自定义工具栏时触发。
InitCustomizationDialog
Public Event InitCustomizationDialog(ByVal hDlg As LongPtr, ByRef HideHelpButton As Boolean)初始化自定义对话框时触发。hDlg为对话框句柄,HideHelpButton控制是否隐藏帮助按钮。
CustomizationChange
Public Event CustomizationChange()自定义工具栏发生改变时触发。
ResetCustomizations
Public Event ResetCustomizations(ByRef CloseDialog As Boolean)重置自定义时触发。CloseDialog控制是否关闭对话框。
CustomizationHelp
Public Event CustomizationHelp()用户在自定义对话框中点击帮助时触发。
EndCustomization
Public Event EndCustomization()结束自定义工具栏时触发。
ButtonClick
Public Event ButtonClick(ByVal Button As TbrButton)用户单击按钮时触发。
ButtonDrag
Public Event ButtonDrag(ByVal Button As TbrButton, ByVal MouseButton As Integer)用户拖动按钮时触发。
ButtonHotChanged
Public Event ButtonHotChanged(ByVal Button As TbrButton, ByVal Hot As Boolean)按钮热点状态改变时触发。
ButtonDropDown
Public Event ButtonDropDown(ByVal Button As TbrButton)下拉按钮被点击时触发。
ButtonMenuClick
Public Event ButtonMenuClick(ByVal ButtonMenu As TbrButtonMenu)下拉菜单项被点击时触发。
ButtonMenuClick2
Public Event ButtonMenuClick2(ByVal Button As TbrButton, ByVal ID As Long)下拉菜单项被点击时触发,同时提供所属按钮和菜单项ID。
ButtonMouseEnter
Public Event ButtonMouseEnter(ByVal Button As TbrButton)鼠标进入按钮区域时触发。
ButtonMouseLeave
Public Event ButtonMouseLeave(ByVal Button As TbrButton)鼠标离开按钮区域时触发。
MouseDown
Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)按下鼠标按钮时触发。
MouseMove
Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)移动鼠标时触发。
MouseUp
Public Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)释放鼠标按钮时触发。
MouseEnter
Public Event MouseEnter()鼠标进入控件区域时触发。
MouseLeave
Public Event MouseLeave()鼠标离开控件区域时触发。
OLECompleteDrag
Public Event OLECompleteDrag(Effect As Long)OLE拖放操作完成时触发。
OLEDragDrop
Public Event OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)OLE拖放操作放置时触发。
OLEDragOver
Public Event OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)OLE拖放操作悬停时触发。
OLEGiveFeedback
Public Event OLEGiveFeedback(Effect As Long, DefaultCursors As Boolean)OLE拖放操作给反馈时触发。
OLESetData
Public Event OLESetData(Data As DataObject, DataFormat As Integer)OLE拖放操作设置数据时触发。
OLEStartDrag
Public Event OLEStartDrag(Data As DataObject, AllowedEffects As Long)OLE拖放操作开始时触发。
子对象
TbrButton 类
工具栏按钮对象。
TbrButton 属性
Index
Public Property Get Index() As Long按钮在集合中的索引。
Key
Public Property Get Key() As String
Public Property Let Key(ByVal Value As String)按钮的唯一标识键。
Tag
Public Property Get Tag() As Variant
Public Property Let Tag(ByVal Value As Variant)额外数据。
ID
Public Property Get ID() As Long按钮ID。
Caption
Public Property Get Caption() As String
Public Property Let Caption(ByVal Value As String)按钮标题。
Style
Public Property Get Style() As TbrButtonStyleConstants
Public Property Let Style(ByVal Value As TbrButtonStyleConstants)按钮样式。
Image
Public Property Get Image() As Variant
Public Property Let Image(ByVal Value As Variant)按钮图像。
ImageIndex
Public Property Get ImageIndex() As Long图像索引。
ToolTipText
Public Property Get ToolTipText() As String
Public Property Let ToolTipText(ByVal Value As String)工具提示文本。
Description
Public Property Get Description() As String
Public Property Let Description(ByVal Value As String)按钮描述。
Value
Public Property Get Value() As TbrButtonValueConstants
Public Property Let Value(ByVal Value As TbrButtonValueConstants)按钮值(按下/未按下状态)。
Enabled
Public Property Get Enabled() As Boolean
Public Property Let Enabled(ByVal Value As Boolean)是否可用。
Visible
Public Property Get Visible() As Boolean
Public Property Let Visible(ByVal Value As Boolean)是否可见。
MixedState
Public Property Get MixedState() As Boolean
Public Property Let MixedState(ByVal Value As Boolean)是否处于混合状态(三态复选框)。
HighLighted
Public Property Get HighLighted() As Boolean
Public Property Let HighLighted(ByVal Value As Boolean)是否高亮显示。
NoImage
Public Property Get NoImage() As Boolean
Public Property Let NoImage(ByVal Value As Boolean)是否不显示图像。
NoPrefix
Public Property Get NoPrefix() As Boolean
Public Property Let NoPrefix(ByVal Value As Boolean)是否不处理助记符前缀(&)。
AutoSize
Public Property Get AutoSize() As Boolean
Public Property Let AutoSize(ByVal Value As Boolean)是否自动调整大小。
CustomWidth
Public Property Get CustomWidth() As Single
Public Property Let CustomWidth(ByVal Value As Single)自定义宽度。
ForeColor
Public Property Get ForeColor() As OLE_COLOR
Public Property Let ForeColor(ByVal Value As OLE_COLOR)前景色。
Position
Public Property Get Position() As Long
Public Property Let Position(ByVal Value As Long)按钮位置。
Hot
Public Property Get Hot() As Boolean是否处于热点状态。
Left
Public Property Get Left() As Single按钮左边距。
Top
Public Property Get Top() As Single按钮顶边距。
Width
Public Property Get Width() As Single按钮宽度。
Height
Public Property Get Height() As Single按钮高度。
ButtonMenus
Public Property Get ButtonMenus() As TbrButtonMenus下拉菜单集合。
hMenu
Public Property Get hMenu() As LongPtr菜单句柄。
TbrButtonMenu 类
按钮下拉菜单项对象。
TbrButtonMenu 属性
Index
Public Property Get Index() As Long菜单项索引。
Key
Public Property Get Key() As String
Public Property Let Key(ByVal Value As String)唯一标识键。
Tag
Public Property Get Tag() As Variant
Public Property Let Tag(ByVal Value As Variant)额外数据。
Text
Public Property Get Text() As String
Public Property Let Text(ByVal Value As String)菜单项文本。
Enabled
Public Property Get Enabled() As Boolean
Public Property Let Enabled(ByVal Value As Boolean)是否可用。
Visible
Public Property Get Visible() As Boolean
Public Property Let Visible(ByVal Value As Boolean)是否可见。
Checked
Public Property Get Checked() As Boolean
Public Property Let Checked(ByVal Value As Boolean)是否选中。
Separator
Public Property Get Separator() As Boolean
Public Property Let Separator(ByVal Value As Boolean)是否为分隔符。
Picture
Public Property Get Picture() As IPictureDisp
Public Property Set Picture(ByVal Value As IPictureDisp)菜单项图标。
Parent
Public Property Get Parent() As TbrButton所属按钮。
TbrButtonMenus 类
按钮下拉菜单项集合。
TbrButtonMenus 成员
NewEnum
Public Function NewEnum() As IUnknown枚举器(隐藏)。
Add
Public Function Add(Optional ByVal Index As Variant, Optional ByVal Key As Variant, Optional ByVal Text As Variant) As TbrButtonMenu添加菜单项。
Item
Public Function Item(ByVal Index As Variant) As TbrButtonMenu获取菜单项(默认成员)。
Exists
Public Function Exists(ByVal Index As Variant) As Boolean检查菜单项是否存在。
Count
Public Property Get Count() As Long菜单项数量。
Clear
Public Sub Clear()清除所有菜单项。
Remove
Public Sub Remove(ByVal Index As Variant)移除菜单项。
TbrButtons 类
工具栏按钮集合。
TbrButtons 成员
NewEnum
Public Function NewEnum() As IUnknown枚举器(隐藏)。
Add
Public Function Add(Optional ByVal Index As Variant, Optional ByVal Key As Variant, Optional ByVal Caption As Variant, Optional ByVal Style As Variant, Optional ByVal Image As Variant) As TbrButton添加按钮。
Item
Public Function Item(ByVal Index As Variant) As TbrButton获取按钮(默认成员)。
Exists
Public Function Exists(ByVal Index As Variant) As Boolean检查按钮是否存在。
Count
Public Property Get Count() As Long按钮数量。
Clear
Public Sub Clear()清除所有按钮。
Remove
Public Sub Remove(ByVal Index As Variant)移除按钮。
TbrButtonProperties 类
按钮内部属性对象(Friend访问)。
FInit
Friend Property Get FInit() As Boolean
Friend Property Let FInit(ByVal Value As Boolean)内部初始化标志。
ForeColor
Public Property Get ForeColor() As OLE_COLOR
Public Property Let ForeColor(ByVal Value As OLE_COLOR)前景色。
代码示例
基本用法
' 创建工具栏并添加按钮
With ToolBar1.Buttons
.Add , "New", "新建", tbrButtonDefault, 1
.Add , "Open", "打开", tbrButtonDefault, 2
.Add , , , tbrButtonSeparator
.Add , "Bold", "加粗", tbrButtonCheck, 3
End With
' 为按钮添加下拉菜单
Dim btn As TbrButton
Set btn = ToolBar1.Buttons.Add(, "Font", "字体", tbrButtonDropDown, 4)
With btn.ButtonMenus
.Add , "Arial", "Arial"
.Add , "Courier", "Courier New"
.Add , , , , , True ' 分隔符
.Add , "Tahoma", "Tahoma"
End With
' 处理按钮点击
Private Sub ToolBar1_ButtonClick(ByVal Button As TbrButton)
Select Case Button.Key
Case "New": MsgBox "新建文件"
Case "Open": MsgBox "打开文件"
Case "Bold": MsgBox "加粗: " & Button.Value
End Select
End Sub
' 处理下拉菜单点击
Private Sub ToolBar1_ButtonMenuClick(ByVal ButtonMenu As TbrButtonMenu)
MsgBox "选择字体: " & ButtonMenu.Text
End Sub