Both menus and toolbars are in fact CommandBar controls, and when you add child controls to a CommandBar you have the option to add the following type of controls:

  • MsoControlType.msoControlActiveX
  • MsoControlType.msoControlButton
  • MsoControlType.msoControlComboBox
  • MsoControlType.msoControlCustom
  • MsoControlType.msoControlDropdown
  • MsoControlType.msoControlEdit
  • MsoControlType.msoControlPopup

 

For both toolbar buttons and menu items you add child controls of type MsoControlType.msoControlButton using the following code

 

Extending Word with VSTO 2005 – Adding icons to toolbars and menus// Create the toolbar
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusOffice.CommandBar toolbar 
= this.Application.CommandBars.Add(
Extending Word with VSTO 2005 – Adding icons to toolbars and menus    
"Toolbar name", MsoBarPosition.msoBarTop, missing, false);
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Add a button to it
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusOffice.CommandBarButton button 
= (Office.CommandBarButton)
Extending Word with VSTO 2005 – Adding icons to toolbars and menus    toolbar.Controls.Add(MsoControlType.msoControlButton, missing,
Extending Word with VSTO 2005 – Adding icons to toolbars and menus    missing, missing, 
false);
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Add the menu
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusOffice.CommandBarPopup menu 
= (Office.CommandBarPopup)
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
this.Application.CommandBars.ActiveMenuBar.Controls.Add(
Extending Word with VSTO 2005 – Adding icons to toolbars and menus    Office.MsoControlType.msoControlPopup, missing, missing, missing, 
false);
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Add a menu item
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusOffice.CommandBarButton menuItem 
= (Office.CommandBarButton) menu.Controls.Add(
Extending Word with VSTO 2005 – Adding icons to toolbars and menus    Office.MsoControlType.msoControlButton, missing, missing, missing, 
false);
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

 
When you have created a msoControlButton, you have the option of using one of the following styles

 

  • MsoButtonStyle.msoButtonAutomatic
  • MsoButtonStyle.msoButtonCaption
  • MsoButtonStyle.msoButtonIcon
  • MsoButtonStyle.msoButtonIconAndCaption
  • MsoButtonStyle.msoButtonIconAndCaptionBelow
  • MsoButtonStyle.msoButtonIconAndWrapCaption
  • MsoButtonStyle.msoButtonIconAndWrapCaptionBelow
  • MsoButtonStyle.msoButtonWrapCaption

 

Anything style with “Icon” its name, obviously supports having an icon displayed. Let’s pick the following as an example

 

Extending Word with VSTO 2005 – Adding icons to toolbars and menusbutton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;

 

There are three ways of adding an icon and the first one is using the FaceId property. This property allows you to specify an index for the icons that are built into Word and Excel and there is around 3000 of them.

 

Extending Word with VSTO 2005 – Adding icons to toolbars and menusbutton.FaceId = 136;

 
The built in icons support transparency and are in fact the ones used by Word and Excel themselves. However, if the build in icons do not satisfy you, you can always create your own and use them instead. This brings us to the second way of adding icons which is using the PasteFace property. This property works in conjunction with the clipboard; you copy an icon into the clipboard and using PasteFace the button acquires the icon from the clipboard.

 

Extending Word with VSTO 2005 – Adding icons to toolbars and menus// Get the icon from the resource file
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusIcon icon 
= this.GetIconFromResource("example.ico");
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Copy the icon into the clipboard
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusClipboard.SetDataObject(icon.ToBitmap(), 
true);
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Paste the icon into the button
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusbutton.PasteFace();
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

 
Unfortunately, this method does not support transparency, so if what you need is to add custom transparent icons, you need to go with option number three which uses STDOLE.

 

First you need to convert your image to IPictureDisp

 

Extending Word with VSTO 2005 – Adding icons to toolbars and menusPublic stdole.IPictureDisp ConvertImage(Image image)
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

 

Now you can set the button’s Picture and Mask property. You have to set the Picture property with the IPictureDisp version of the icon you would like to appear on the button and the Mask property with the IPictureDisp version of the icon’s mask. The icon’s mask is just another icon which has all of the transparent areas of the original icon filled with the white and the rest of the icon filled with black.

 

Extending Word with VSTO 2005 – Adding icons to toolbars and menus// Get the icon and its mask from the resource file
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusImage icon 
= this.GetImageFromResource("example.ico");
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menusImage iconMask 
= this.GetImageMaskFromResource("example_mask.ico");
Extending Word with VSTO 2005 – Adding icons to toolbars and menusExtending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Set its icon and its mask so that the transparency works correctly
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
// Use the ConvertImage method to get the IPictureDisp version of the the images
Extending Word with VSTO 2005 – Adding icons to toolbars and menus

Extending Word with VSTO 2005 – Adding icons to toolbars and menusbutton.Picture 
= this.ConvertImage(icon);
Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menusbutton.Mask 
= this.ConvertImage(iconMask);Extending Word with VSTO 2005 – Adding icons to toolbars and menus
Extending Word with VSTO 2005 – Adding icons to toolbars and menus


 来源:  Adding Picture To Button On OutLook CommandBar

相关文章: