软件以用为本

不管黄猫白猫,逮着老鼠就是好猫!

导航

Extending Word with VSTO 2005 – Adding icons to toolbars and menus

 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

 

// Create the toolbar

Office.CommandBar toolbar 
= this.Application.CommandBars.Add(
    
"Toolbar name", MsoBarPosition.msoBarTop, missing, false);

// Add a button to it

Office.CommandBarButton button 
= (Office.CommandBarButton)
    toolbar.Controls.Add(MsoControlType.msoControlButton, missing,
    missing, missing, 
false);

// Add the menu

Office.CommandBarPopup menu 
= (Office.CommandBarPopup)
this.Application.CommandBars.ActiveMenuBar.Controls.Add(
    Office.MsoControlType.msoControlPopup, missing, missing, missing, 
false);

// Add a menu item

Office.CommandBarButton menuItem 
= (Office.CommandBarButton) menu.Controls.Add(
    Office.MsoControlType.msoControlButton, missing, missing, missing, 
false);

 
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

 

button.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.

 

button.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.

 

// Get the icon from the resource file

Icon icon 
= this.GetIconFromResource("example.ico");

// Copy the icon into the clipboard

Clipboard.SetDataObject(icon.ToBitmap(), 
true);

// Paste the icon into the button

button.PasteFace();

 
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

 

Public stdole.IPictureDisp ConvertImage(Image image)

{

    
//Returns a image to STDOLE format

    
return (stdole.IPictureDisp)AxHost.GetIPictureDispFromPicture(image);

}

 

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.

 

// Get the icon and its mask from the resource file

Image icon 
= this.GetImageFromResource("example.ico");

Image iconMask 
= this.GetImageMaskFromResource("example_mask.ico");

// Set its icon and its mask so that the transparency works correctly

// Use the ConvertImage method to get the IPictureDisp version of the the images

button.Picture 
= this.ConvertImage(icon);

button.Mask 
= this.ConvertImage(iconMask);


 来源:  Extending Word with VSTO 2005 – Adding icons to toolbars and menus
其他有关链接:Adding Picture To Button On OutLook CommandBar

posted on 2006-10-20 08:57  Jinsong Huang  阅读(1036)  评论(0编辑  收藏  举报