【问题标题】:Outlook 2007 Add-in: How to add an icon to an msoControlButtonOutlook 2007 加载项:如何将图标添加到 msoControlButton
【发布时间】:2012-03-31 02:40:16
【问题描述】:

背景:我正在用 C# 在 VS2010 中开发 Outlook 2007 插件。我正在做的具体事情是将菜单项添加到与电子邮件关联的上下文菜单中。我使用以下代码执行此操作:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
   Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

private void Application_ItemContextMenuDisplay(Office.CommandBar commandBar, Outlook.Selection selection)
{
   var cmdButtonCallContact = (Office.CommandBarButton)commandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, System.Reflection.Missing.Value, 6, System.Reflection.Missing.Value);

   cmdButtonCallContact.Caption = "&Foo";
   //cmdButtonCallContact.Picture = ?
   cmdButtonCallContact.Click += cmdButtonCopy_Click;
}

private void cmdButtonCopy_Click(Office.CommandBarButton ctrl, ref bool canceldefault)
{
   System.Windows.Forms.MessageBox.Show("Bar");
}

问题:似乎无法设置图片。 Msdn 示例依赖于我没有的 AxHost 转换函数。有没有一种直接将图像或位图设置为图片的方法?

谢谢。

【问题讨论】:

    标签: c# .net outlook-addin office-addins


    【解决方案1】:

    如果您想要自定义图像,您必须依赖 AxHost 方法 (see MSDN reference) 或 PictureDispConverter,这是基于 OleCreatePictureIndirect 的另一种方法 created by Microsoft

    如果你想使用内置图标,你可以设置FaceId。下载 Office Icons Gallery 以查看 Office 2007 FaceId 值。

    【讨论】:

    【解决方案2】:

    以下代码使用System.Drawing.Bitmap(存储为资源)并将其转换为可分配给Office.CommandBarButton.Picture的图像

    private Office.CommandBarButton buttonOne;
    void createbutton()
    {
        Office.CommandBar newMenuBar = Inspector.CommandBars.Add("EAD", Office.MsoBarPosition.msoBarTop, false, true);
        buttonOne = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, missing, missing, true);buttonOne.Caption = "Ansari";
        buttonOne.Style = Office.MsoButtonStyle.msoButtonIconAndWrapCaptionBelow;                   
    
        buttonOne.Picture = getImage();
        //Register send event handler
        buttonOne.Click += buttonOne_Click;
        newMenuBar.Visible = true;
    }
    void buttonOne_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
    {
        MessageBox.Show("Hi");
    }
    private stdole.IPictureDisp getImage()
    {
        stdole.IPictureDisp tempImage = null;
        try
        {
            System.Drawing.Bitmap 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;
    }
    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);
        }
    }     
    

    注意:在资源中添加名称为 Icon1 的图像。

    【讨论】:

      【解决方案3】:

      仅供参考,如果您想将任何 Office 内置图像应用到您的按钮(在 here 中查看图像库),您只需调用 GetImageMso() 方法即可。

      CommandBarButton.Picture = Application.CommandBars.GetImageMso("ImageMSO", 16, 16);
      

      这是使用FaceID 属性的另一种方法。

      【讨论】:

      • 我试过那个,但发现图像并没有真正显示正确,至少在 2007 年。它似乎失去了透明属性,所以你在图像周围得到了一个轮廓。此外,在 Outlook 中,您需要多一层重定向才能调用该函数: Globals.Addin.Application.ActiveExplorer().CommandBars.GetImageMso("AcceptInvitation", 16, 16);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多