Gastiflex

Hi,

I'm working on a VSTO 2005 SE add-in for Word 2007 that add items to the context menu. My problem is that I didn't manage to add images to my new context menu items.

Here's my code to add an item to the context menu (here, the "Text" context menu) :

Code Snippet

Dim myContextMenuItem As Office.CommandBar

Dim btn As Office.CommandBarButton

myContextMenuItem = app.CommandBars("Text")

btn = myContextMenuItem.FindControl(Office.MsoControlType.msoControlButton, , Caption)

If (btn Is Nothing) Then

btn = myContextMenuItem.Controls.Add(Office.MsoControlType.msoControlButton, , "", pos, True)

btn.Caption = Caption

btn.Tag = Caption

btn.Style = Office.MsoButtonStyle.msoButtonCaption

btn.Visible = False

End If

I guess I should use the Picture property. So I added the following line :

btn.Picture = GetImage("myImage")

Where GetImage is declared like this :

Code Snippet

Public Function GetImage(ByVal imageName As String) As stdole.IPictureDisp

Dim bitmap As Bitmap

bitmap = My.Resources.ResourceManager.GetObject(imageName)

Return PictureConverter.ImageToPictureDisp(bitmap)

End Function

And PictureConverter is a friend class :

Code Snippet

Friend Class PictureConverter

Inherits AxHost

Private Sub New()

MyBase.New(String.Empty)

End Sub

Public Shared Function ImageToPictureDisp( _

ByVal image As Image) As stdole.IPictureDisp

Return CType(GetIPictureDispFromPicture(image), _

stdole.IPictureDisp)

End Function

Public Shared Function IconToPictureDisp( _

ByVal icon As Icon) As stdole.IPictureDisp

Return ImageToPictureDisp(icon.ToBitmap())

End Function

Public Shared Function PictureDispToImage( _

ByVal picture As stdole.IPictureDisp) As Image

Return GetPictureFromIPicture(picture)

End Function

End Class

Unfortunately, this doesn't work. There's no crash, but there's no picture in my context menu item.


Re: Visual Studio Tools for Office Adding images to home-made context menus

Ji Zhou ¨C MSFT

Hi,

I don't have a test. But first glance of you code. I think you should set the btn.Style to Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption.

Please modify and test if it works.

Thanks

Ji






Re: Visual Studio Tools for Office Adding images to home-made context menus

Gastiflex

Thank you, it works. Another solution is to forget the Style property and it works as well.




Re: Visual Studio Tools for Office Adding images to home-made context menus

Gastiflex

By the way, how can I deal with transparency
My images have been created with Photoshop, and they have a transparent backgroup. I've saved them as a .png. When I affect them to a button in a ribbon, the transparency works, but when I affect them to a context menu item, I've got an awfull blue background instead of a transparent one.




Re: Visual Studio Tools for Office Adding images to home-made context menus

Ji Zhou ¨C MSFT

Hi,

Please check this KB link : http://support.microsoft.com/kb/288771

Thanks

Ji