ElektroStudios
ElektroStudios

Reputation: 20464

VS 2013 SDK: How to set a CommandBarButton picture?

Following the C# answer of @Carlos Quintero in this other question and next reading him C# article here in this link, I've tried to follow the steps mentioned to set the CommandBarButton.Picture of a menu button, however, when I do it any image appears, the package does not throw any error, just the image is not shown (maybe is not properly loaded or its in bad format?)

These are the images that I'm trying to set:

enter image description here enter image description here enter image description here

And this is the code that I'm using, what I'm missing or doing wrong?:

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

Upvotes: 0

Views: 423

Answers (2)

Carlos Quintero
Carlos Quintero

Reputation: 4414

If you are creating a package, the user interface should be created using the .vsct file, which allows you to declare command pictures. See Howto add icons to commands on toolbars

Upvotes: 1

ElektroStudios
ElektroStudios

Reputation: 20464

The solution was really easy, just the Style property of the CommandBarButton should be set as MsoButtonStyle.msoButtonIconAndCaption instead of MsoButtonStyle.msoButtonIcon.

Upvotes: 1

Related Questions