Share via


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

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 应用程序级项目

Microsoft Office 版本

  • Outlook 2003

  • Outlook 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

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

添加自定义图标

  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
            MessageBox.Show(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
            MessageBox.Show(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)
        {
            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)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
  2. 创建一个名为 ConvertImage 的新类。该类使用 System.Forms.Axhost 将 Image 文件转换成可以应用于菜单项的图像类型。

    <Microsoft.VisualStudio.Tools.Applications.Runtime.StartupObjectAttribute(0), _
     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 Icon = My.Resources.Icon1
            Dim newImageList As New ImageList
            newImageList.Images.Add(newIcon)
            tempImage = ConvertImage.Convert(newImageList.Images(0))
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return tempImage
    End Function
    
    private stdole.IPictureDisp getImage()
    {
        stdole.IPictureDisp tempImage = null;
        try
        {
            System.Drawing.Icon newIcon = 
                Properties.Resources.Icon1;
    
            ImageList newImageList = new ImageList();
            newImageList.Images.Add(newIcon);
            tempImage = ConvertImage.Convert(newImageList.Images[0]);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return tempImage;
    }
    

编译代码

此示例需要:

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

请参见

任务

如何:向 Outlook 添加自定义工具栏和工具栏项

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

如何:添加或移除资源

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

概念

Outlook 对象模型概述

其他资源

管理应用程序资源