VS 2013 SDK:如何设置 CommandBarButton 图片?
VS 2013 SDK: How to set a CommandBarButton picture?
在 this other question and next reading him C# article here in this link 中@Carlos Quintero 的 C# 回答之后,我尝试按照提到的步骤进行操作设置菜单按钮的 CommandBarButton.Picture
,但是,当我这样做时,会出现任何图像,程序包不会抛出任何错误,只是图像未显示(可能未正确加载或格式错误?)
这些是我要设置的图像:
这是我正在使用的代码,我遗漏了什么或做错了什么?:
Friend Sub CreateMenu(ByVal dte As EnvDTE80.DTE2)
Dim objImageToPictureDispConverter As ImageToPictureDispConverter = Nothing
Dim objImage As System.Drawing.Image = Nothing
Dim objIPictureDisp As stdole.IPictureDisp = Nothing
objImage = System.Drawing.Image.FromFile("C:\path to resources...\Menu_Tag_Green.png")
objImageToPictureDispConverter = New ImageToPictureDispConverter
objIPictureDisp = objImageToPictureDispConverter.GetIPictureDispFromImage(objImage)
objImageToPictureDispConverter.Dispose()
' Get a reference to the context menu of code window.
Dim codeWindowCommandBar As CommandBar =
DirectCast(dte.CommandBars, CommandBars)("Code Window")
' Add a popup command bar.
Dim mainPopup As CommandBarPopup =
DirectCast(codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing), CommandBarPopup)
mainPopup.Caption = "..."
' Add controls to the popup command bar.
BtMakeCodeExample =
DirectCast(mainPopup.Controls.Add(MsoControlType.msoControlButton,
Missing.Value, Missing.Value,
1, True), CommandBarButton)
BtMakeCodeExample.Caption = "My Button"
BtMakeCodeExample.Style = MsoButtonStyle.msoButtonIcon
BtMakeCodeExample.Picture = objIPictureDisp
End Sub
+
Public Class ImageToPictureDispConverter : Inherits System.Windows.Forms.AxHost
Public Sub New()
MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
End Sub
Public Function GetIPictureDispFromImage(ByVal objImage As System.Drawing.Image) As stdole.IPictureDisp
Dim objPicture As stdole.IPictureDisp
objPicture = CType(System.Windows.Forms.AxHost.GetIPictureDispFromPicture(objImage), stdole.IPictureDisp)
Return objPicture
End Function
End Class
解决方案非常简单,只需将 CommandBarButton
的 Style
属性 设置为 MsoButtonStyle.msoButtonIconAndCaption
而不是 MsoButtonStyle.msoButtonIcon
。
如果您正在创建包,则应使用 .vsct 文件创建用户界面,该文件允许您声明命令图片。参见 Howto add icons to commands on toolbars
在 this other question and next reading him C# article here in this link 中@Carlos Quintero 的 C# 回答之后,我尝试按照提到的步骤进行操作设置菜单按钮的 CommandBarButton.Picture
,但是,当我这样做时,会出现任何图像,程序包不会抛出任何错误,只是图像未显示(可能未正确加载或格式错误?)
这些是我要设置的图像:
这是我正在使用的代码,我遗漏了什么或做错了什么?:
Friend Sub CreateMenu(ByVal dte As EnvDTE80.DTE2)
Dim objImageToPictureDispConverter As ImageToPictureDispConverter = Nothing
Dim objImage As System.Drawing.Image = Nothing
Dim objIPictureDisp As stdole.IPictureDisp = Nothing
objImage = System.Drawing.Image.FromFile("C:\path to resources...\Menu_Tag_Green.png")
objImageToPictureDispConverter = New ImageToPictureDispConverter
objIPictureDisp = objImageToPictureDispConverter.GetIPictureDispFromImage(objImage)
objImageToPictureDispConverter.Dispose()
' Get a reference to the context menu of code window.
Dim codeWindowCommandBar As CommandBar =
DirectCast(dte.CommandBars, CommandBars)("Code Window")
' Add a popup command bar.
Dim mainPopup As CommandBarPopup =
DirectCast(codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing), CommandBarPopup)
mainPopup.Caption = "..."
' Add controls to the popup command bar.
BtMakeCodeExample =
DirectCast(mainPopup.Controls.Add(MsoControlType.msoControlButton,
Missing.Value, Missing.Value,
1, True), CommandBarButton)
BtMakeCodeExample.Caption = "My Button"
BtMakeCodeExample.Style = MsoButtonStyle.msoButtonIcon
BtMakeCodeExample.Picture = objIPictureDisp
End Sub
+
Public Class ImageToPictureDispConverter : Inherits System.Windows.Forms.AxHost
Public Sub New()
MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
End Sub
Public Function GetIPictureDispFromImage(ByVal objImage As System.Drawing.Image) As stdole.IPictureDisp
Dim objPicture As stdole.IPictureDisp
objPicture = CType(System.Windows.Forms.AxHost.GetIPictureDispFromPicture(objImage), stdole.IPictureDisp)
Return objPicture
End Function
End Class
解决方案非常简单,只需将 CommandBarButton
的 Style
属性 设置为 MsoButtonStyle.msoButtonIconAndCaption
而不是 MsoButtonStyle.msoButtonIcon
。
如果您正在创建包,则应使用 .vsct 文件创建用户界面,该文件允许您声明命令图片。参见 Howto add icons to commands on toolbars