HOW TO:將自訂圖示加入至工具列和功能表項目
這個範例會將圖示加入至 Microsoft Office Outlook 之自訂功能表上的命令列按鈕中。 此圖示會包含在專案資源內。 如需專案資源的詳細資訊,請參閱Adding and Editing Resources (Visual C#) 和 HOW TO:加入或移除資源。
**適用於:**本主題中的資訊適用於下列應用程式的應用程式層級專案:InfoPath 2007、Outlook 2007、Project 2007 和 Visio 2007。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
雖然這是 Outlook 專屬範例,但是程式碼中將圖示加入至命令列按鈕的部分,還是可以用來將圖示加入至以上所列任何應用程式中的命令列按鈕。
若要加入自訂圖示
請將程式碼加入至 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); } }
建立一個名為 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); } }
加入方法,以便將圖示檔轉換成 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 的圖示。
請參閱
工作
HOW TO:將自訂功能表和功能表項目加入至 Outlook
HOW TO:指定應用程式圖示 (Visual Basic、C#)