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
    
    
    
            private Office.CommandBar menuBar;
            private Office.CommandBarPopup newMenuBar;
            private Office.CommandBarButton buttonOne;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                AddMenuBar();
            }
    
            private void AddMenuBar()
            {
                try
                {
                    menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
                    newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                        Office.MsoControlType.msoControlPopup, missing,
                        missing, missing, true);
                    if (newMenuBar != null)
                    {
                        newMenuBar.Caption = "See New Icon";
                        newMenuBar.Tag = menuTag;
                        buttonOne = (Office.CommandBarButton)
                            newMenuBar.Controls.
                        Add(Office.MsoControlType.msoControlButton, System.
                            Type.Missing, System.Type.Missing, 1, true);
                        buttonOne.Style = Office.MsoButtonStyle.
                            msoButtonIconAndCaption;
                        buttonOne.Caption = "New Icon";
                        buttonOne.FaceId = 65;
                        buttonOne.Tag = "c123";
                        buttonOne.Picture = getImage();
                        newMenuBar.Visible = true;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
    
    
  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
    
    sealed public class ConvertImage : System.Windows.Forms.AxHost
    {
        private ConvertImage()
            : base(null)
        {
        }
    
        public static stdole.IPictureDisp Convert
            (System.Drawing.Image image)
        {
            return (stdole.IPictureDisp)System.
                Windows.Forms.AxHost
                .GetIPictureDispFromPicture(image);
        }
    }
    
  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
    
    private stdole.IPictureDisp getImage()
    {
        stdole.IPictureDisp tempImage = null;
        try
        {
            System.Drawing.Icon newIcon =
                Properties.Resources.Icon1;
    
            System.Windows.Forms.ImageList newImageList = 
                new System.Windows.Forms.ImageList();
            newImageList.Images.Add(newIcon);
            tempImage = ConvertImage.Convert(newImageList.Images[0]);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        return tempImage;
    }
    

Compiling the Code

This example requires:

  • An icon named Icon1 in the project resources.

See Also

Tasks

How to: Create Office Toolbars

How to: Add Custom Menus and Menu Items to Outlook

How to: Add or Remove Resources

How to: Specify an Application Icon (Visual Basic, C#)

Other Resources

Outlook Object Model Overview

Managing Application Resources