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 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.

    
    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 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;
    }
    
    
    

This example requires:

  • An icon named Icon1 in the project resources.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft