6

Background: I'm developing an Outlook 2007 Add-in in VS2010 in C#. The specific thing that I'm doing is adding a menu-item to the context menu associated with an email. I do this with the following code:

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

Problem: Can't seem to set the picture. Msdn examples rely on AxHost conversion functions that I don't have. Is there a straightforward way to just set an Image or BitMap to Picture?

Thanks.

h3n
  • 880
  • 1
  • 10
  • 26
kmarks2
  • 4,755
  • 10
  • 48
  • 77

3 Answers3

6

If you want a custom image you have to rely on AxHost approach (see MSDN reference) or PictureDispConverter which is another approach created by Microsoft based on OleCreatePictureIndirect.

If you want to use the built-in icons you can just set the FaceId. Download Office Icons Gallery to view Office 2007 FaceId values.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • The Office Icons Gallery doesn't seem to have FaceIds anymore. Just the new style named icons. They do seem to have them on http://www.outlookexchange.com/articles/toddwalker/BuiltInOLKIcons.asp though. – Wade Hatler Jul 22 '14 at 21:14
3

The following code uses a System.Drawing.Bitmap (stored as a Resource) and converts it to an image, that is assignable to 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);
    }
}     

Note: Add image with name Icon1 in resource.

h3n
  • 880
  • 1
  • 10
  • 26
Sharique Ansari
  • 1,458
  • 1
  • 12
  • 22
1

Just FYI, if you want to apply any office built-in images to your button (view the image gallery in here), you can simply call GetImageMso() method.

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

This is an alternative approach to using FaceID property.

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
  • I tried that one but found that the image doesn't really display right, at least in 2007. It seems to lose the transparent attribute, so you get an outline around the image. Also, in Outlook you need one more level of redirection to call the function: Globals.Addin.Application.ActiveExplorer().CommandBars.GetImageMso("AcceptInvitation", 16, 16); – Wade Hatler Jul 22 '14 at 21:07