VBA:创建内存中的位图以显示为commandbarbutton上的图标
在VBA中,我创建了一个带有按钮的自定义命令栏,允许我在MS Project planning中设置所选线条的背景色 我不想使用文本标题,而是希望在每个按钮上显示具有适当颜色的16x16位图图标。我可以从硬盘加载这些位图,但希望在VBA中创建它们 这可能吗?经过广泛的研究,VBA afaik中没有位图或绘图类 现在的情况是: 它应该变成什么(手动完成): 代码:VBA:创建内存中的位图以显示为commandbarbutton上的图标,vba,bitmap,ms-project,Vba,Bitmap,Ms Project,在VBA中,我创建了一个带有按钮的自定义命令栏,允许我在MS Project planning中设置所选线条的背景色 我不想使用文本标题,而是希望在每个按钮上显示具有适当颜色的16x16位图图标。我可以从硬盘加载这些位图,但希望在VBA中创建它们 这可能吗?经过广泛的研究,VBA afaik中没有位图或绘图类 现在的情况是: 它应该变成什么(手动完成): 代码: 我猜cctrl是作为命令按钮的Dim cctrl中的一个输入错误。另外,为什么不将位图图标存储在隐藏的工作表中,然后将其加载到按钮
我猜
cctrl
是作为命令按钮的Dim cctrl中的一个输入错误。另外,为什么不将位图图标存储在隐藏的工作表中,然后将其加载到按钮?或用户窗体中的隐藏图像控件,从中可以将图像导出到用户的临时目录,然后将其上载到。Picture
?cctrl确实是一个打字错误,explicit没有打开,所以它没有返回任何错误-你能给我看一些关于如何将隐藏在用户表单中的图像存储到tempdir的示例代码吗?Chip Pearson发布了如何按照@SiddharthRout的建议执行此操作:
'1 red
'2 yellow
'3 green
'4 blue
'5 dark blue
'6 fushia
'7 white
Sub createcolorbar()
Dim cbar As CommandBar
Dim cctrl As CommandBarButton
On Error Resume Next
CommandBars("Colors").Delete
Set cbar = CommandBars.Add("Colors")
cbar.Visible = True
cbar.Position = msoBarTop
With cbar
'white
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "White"
.State = msoButtonUp
.Style = msoButtonIconAndCaption 'change for icon
.OnAction = "Macro ""color_white"""
'.FaceId = 59 'Smiley
.Picture = stdole.StdFunctions.LoadPicture("c:\Temp\a.bmp")
End With
' red
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "Red"
.State = msoButtonUp
.Style = msoButtonCaption
.OnAction = "Macro ""color_red"""
End With
'yellow
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "Yellow"
.State = msoButtonUp
.Style = msoButtonCaption
.OnAction = "Macro ""color_yellow"""
End With
'green
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "Green"
.State = msoButtonUp
.Style = msoButtonCaption
.OnAction = "Macro ""color_green"""
End With
'blue
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "Blue"
.State = msoButtonUp
.Style = msoButtonCaption
.OnAction = "Macro ""color_blue"""
End With
'Darkblue
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "Darkblue"
.State = msoButtonUp
.Style = msoButtonCaption
.OnAction = "Macro ""color_darkblue"""
End With
'fushia
Set ctrl = .Controls.Add(msoControlButton, , , , True)
With ctrl
.BeginGroup = True
.Caption = "Fushia"
.State = msoButtonUp
.Style = msoButtonCaption
.OnAction = "Macro ""color_fushia"""
End With
End With
Set ctrl = Nothing
End Sub
Sub color_x(x)
Set ts = ActiveSelection.Tasks
For Each tsk In ts
FontEx CellColor:=x
Next tsk
End Sub
Sub color_red()
Call color_x(1)
End Sub
Sub color_yellow()
Call color_x(2)
End Sub
Sub color_green()
Call color_x(3)
End Sub
Sub color_blue()
Call color_x(4)
End Sub
Sub color_darkblue()
Call color_x(5)
End Sub
Sub color_fushia()
Call color_x(6)
End Sub
Sub color_white()
Call color_x(7)
End Sub