如何:向工具栏和菜单项添加自定义图标

本示例在 Microsoft Office Outlook 中将图标添加到自定义菜单上的命令栏按钮。 项目资源中包括该图标。 有关项目资源的更多信息,请参见Adding and Editing Resources (Visual C#)如何:添加或移除资源

**适用于:**本主题中的信息适用于以下应用程序的应用程序级项目:InfoPath 2007、Outlook 2007、Project 2007 和 Visio 2007。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

尽管这是特定于 Outlook 的示例,但用于将图标添加到命令栏按钮的这部分代码可用于在上面列出的任何应用程序中将图标添加到命令栏按钮。

添加自定义图标

  1. 将代码添加到 Outlook 项目的 ThisAddIn.vb 或 ThisAddIn.cs 文件,以创建表示自定义菜单和菜单命令的 CommandBarPopupCommandBarButton 控件。 此代码检查菜单是否存在。 如果存在,此代码会移除菜单。 然后再添加新菜单。

    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
        RemoveMenubar()
        AddMenuBar()
    End Sub
    
    Private Sub AddMenuBar()
        Try
            MenuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
            newMenuBar = MenuBar.Controls.Add( _
                Office.MsoControlType.msoControlPopup, _
                Temporary:=False)
            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 Sub RemoveMenubar()
        Try
            ' If the menu already exists, remove it.
            Dim foundMenu As Office.CommandBarPopup = _
                Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar. _
                FindControl(Office.MsoControlType.msoControlPopup, _
                System.Type.Missing, menuTag, True, True)
            If foundMenu IsNot Nothing Then
                foundMenu.Delete(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 string menuTag = "AUniqueTag";
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        RemoveMenubar();
        AddMenuBar();
    }
    
    private void AddMenuBar()
    {
        try
        {
            menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
            newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                Office.MsoControlType.msoControlPopup, missing,
                missing, missing, false);
            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);
        }
    }
    
    private void RemoveMenubar()
    {
        // If the menu already exists, remove it.
        try
        {
            Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                FindControl(Office.MsoControlType.msoControlPopup,
                System.Type.Missing, menuTag, true, true);
            if (foundMenu != null)
            {
                foundMenu.Delete(true);
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }
    
  2. 创建一个名为 ConvertImage 的新类。 该类使用 System.Forms.Axhost 将 Image 文件转换成可以应用于菜单项的图像类型。

    <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. 添加一个方法以将图标文件转换成 Image 文件(通过将它添加到 ImageList 来实现)。 此代码会将 Image 文件发送到您创建的 ConvertImage.Convert 方法,再将该文件返回调用方。

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

编译代码

此示例需要:

  • 项目资源中名为 Icon1 的图标。

请参见

任务

如何:创建 Office 工具栏

如何:向 Outlook 添加自定义菜单和菜单项

如何:添加或移除资源

如何:指定应用程序图标(Visual Basic、C#)

其他资源

Outlook 对象模型概述

管理应用程序资源