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

 

None.gif //  Create the toolbar
None.gif

None.gifOffice.CommandBar toolbar 
=   this .Application.CommandBars.Add(
None.gif    
" Toolbar name " , MsoBarPosition.msoBarTop, missing,  false );
None.gif
None.gif
//  Add a button to it
None.gif

None.gifOffice.CommandBarButton button 
=  (Office.CommandBarButton)
None.gif    toolbar.Controls.Add(MsoControlType.msoControlButton, missing,
None.gif    missing, missing, 
false );
None.gif
None.gif
//  Add the menu
None.gif

None.gifOffice.CommandBarPopup menu 
=  (Office.CommandBarPopup)
None.gif
this .Application.CommandBars.ActiveMenuBar.Controls.Add(
None.gif    Office.MsoControlType.msoControlPopup, missing, missing, missing, 
false );
None.gif
None.gif
//  Add a menu item
None.gif

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

 
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

 

None.gif 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.

 

None.gif 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.

 

None.gif //  Get the icon from the resource file
None.gif

None.gifIcon icon 
=   this .GetIconFromResource( " example.ico " );
None.gif
None.gif
//  Copy the icon into the clipboard
None.gif

None.gifClipboard.SetDataObject(icon.ToBitmap(), 
true );
None.gif
None.gif
//  Paste the icon into the button
None.gif

None.gifbutton.PasteFace();
None.gif

 
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

 

None.gif Public stdole.IPictureDisp ConvertImage(Image image)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
//Returns a image to STDOLE format
InBlock.gif

InBlock.gif    
return (stdole.IPictureDisp)AxHost.GetIPictureDispFromPicture(image);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

 

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.

 

None.gif //  Get the icon and its mask from the resource file
None.gif

None.gifImage icon 
=   this .GetImageFromResource( " example.ico " );
None.gif
None.gifImage iconMask 
=   this .GetImageMaskFromResource( " example_mask.ico " );
None.gifNone.gif
None.gif
//  Set its icon and its mask so that the transparency works correctly
None.gif
None.gif
//  Use the ConvertImage method to get the IPictureDisp version of the the images
None.gif

None.gifbutton.Picture 
=   this .ConvertImage(icon);
None.gif
None.gifbutton.Mask 
=   this .ConvertImage(iconMask);None.gif
None.gif


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

转载于:https://www.cnblogs.com/ycat/archive/2006/10/20/534317.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值