Export (0) Print
Expand All

How to: Add Custom Icons to Toolbar and Menu Items

This example adds an icon to a command bar button on a custom menu in Microsoft Office Outlook. The icon is included in the project resources. For more information about project resources, see Creating and Using Resources (Visual C#) and How to: Add or Remove Resources.

Applies to: The information in this topic applies to application-level projects for the following applications: InfoPath 2007, Outlook 2007, Project 2007, and Visio 2007. For more information, see Features Available by Office Application and Project Type.

Although this example is specific to Outlook, the portion of this code that adds an icon to a command bar button can be used to add icons to a command bar button in any of the applications listed above.

To add custom icons

  1. Add code to the Outlook project's ThisAddIn.vb or ThisAddIn.cs file to create CommandBarPopup and CommandBarButton controls that represent the custom menu and menu command. This code adds the new menu.

    
        Private MenuBar As Office.CommandBar
        Private newMenuBar As Office.CommandBarPopup
        Private ButtonOne As Office.CommandBarButton
        Private menuTag As String = "AUniqueName"
    
    
        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            AddMenuBar()
        End Sub
    
        Private Sub AddMenuBar()
            Try
                MenuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
                newMenuBar = MenuBar.Controls.Add( _
                    Office.MsoControlType.msoControlPopup, _
                    Temporary:=True)
                If newMenuBar IsNot Nothing Then
                    newMenuBar.Caption = "See New Icon"
                    newMenuBar.Tag = menuTag
                    ButtonOne = newMenuBar.Controls.Add( _
                        Office.MsoControlType.msoControlButton, _
                        Before:=1, Temporary:=False)
                    With ButtonOne
                        .Style = Office.MsoButtonStyle _
                        .msoButtonIconAndCaption
                        .Caption = "New Icon"
                        .FaceId = 65
                        .Tag = "c123"
                        .Picture = getImage()
                    End With
                    newMenuBar.Visible = True
                End If
            Catch Ex As Exception
                MsgBox(Ex.Message)
            End Try
        End Sub
    
    
    
    
  2. Create a new class named ConvertImage. This class uses System.Forms.Axhost to convert an Image file to an image type that can be applied to the menu item.

    
    <Global.System.Security.Permissions.PermissionSetAttribute _
     (Global.System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class ConvertImage
        Inherits System.Windows.Forms.AxHost
        Public Sub New()
            MyBase.New("59EE46BA-677D-4d20-BF10-8D8067CB8B32")
        End Sub
    
        Public Shared Function Convert(ByVal Image _
            As System.Drawing.Image) As stdole.IPictureDisp
            Convert = GetIPictureFromPicture(Image)
        End Function
    End Class
    
    
    
  3. Add a method to convert the icon file into an Image file by adding it to an ImageList. This code sends the Image file to the ConvertImage.Convert method you created and then returns the file to the caller.

    
    Private Function getImage() As stdole.IPictureDisp
        Dim tempImage As stdole.IPictureDisp = Nothing
        Try
            Dim newIcon As System.Drawing.Icon = My.Resources.Icon1
            Dim newImageList As New System.Windows.Forms.ImageList
            newImageList.Images.Add(newIcon)
            tempImage = ConvertImage.Convert(newImageList.Images(0))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return tempImage
    End Function
    
    
    

This example requires:

  • An icon named Icon1 in the project resources.

Community Additions

ADD
Show:
© 2015 Microsoft