方法 : カスタム アイコンをツール バーおよびメニュー項目に追加する

この例では、Microsoft Office Outlook のカスタム メニューのコマンド バー ボタンにアイコンを追加します。 アイコンは、プロジェクト リソースに含まれています。 プロジェクト リソースの詳細については、「リソースの追加と編集 (Visual C#)」および「方法 : リソースを追加または削除する」を参照してください。

対象: このトピックの情報は、InfoPath 2007、Outlook 2007、Project 2007、および Visio 2007 のアプリケーション レベルのプロジェクトに適用されます。詳細については、「Office アプリケーションおよびプロジェクト タイプ別の使用可能な機能」を参照してください。

この例は Outlook のみを扱っていますが、このコードのコマンド バー ボタンにアイコンを追加する部分は、上記の任意のアプリケーションのコマンド バー ボタンにアイコンを追加するためにも使用できます。

カスタム アイコンを追加するには

  1. Outlook プロジェクトのファイル ThisAddIn.vb または ThisAddIn.cs にコードを追加して、カスタム メニューとメニュー コマンドを表す CommandBarPopup コントロールと CommandBarButton コントロールを作成します。 このコードは、メニューが存在するかどうかを確認します。 存在する場合、そのメニューは削除されます。 その後、新しいメニューが追加されます。

    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 メソッドに Image ファイルを渡してから、そのファイルを呼び出し元に返します。

    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 オブジェクト モデルの概要

その他の技術情報

アプリケーション リソースの管理