Outlook 2007 Add-in: How to Add Icon in msoControlButton

Reference Information. I am developing an Outlook 2007 add-in in VS2010 in C #. The specific thing I'm doing is adding a menu item to the email related context menu. I am doing 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 image. Msdn examples rely on AxHost conversion functions that I don't have. Is there an easy way to just set an image or bitmap to an image?

Thanks.

+6
source share
3 answers

If you need a custom image, you should rely on the AxHost approach ( see the MSDN link ) or the PictureDispConverter , which is another approach created by Microsoft based on OleCreatePictureIndirect .

If you want to use the built-in icons, you can simply install FaceId . Download the Office Icon Gallery to view Office 2007 FaceId .

+6
source

The following code uses System.Drawing.Bitmap (stored as a resource) and converts to an image that is assigned 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 an image named Icon1 in the resource.

+3
source

Just FYI, if you want to apply any office embedded images to your button (see the image gallery in here ), you can just call the GetImageMso () method.

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

This is an alternative approach to using the FaceID property.

+1
source

Source: https://habr.com/ru/post/910723/


All Articles