Mark Lalor Mark Lalor - 3 months ago 9
C# Question

Visual Studio Images with Ribbons (XML)

I'm making Office 2007 addins and I'm trying to use XML instead of the visual designer to customize the ribbon but for some reason I cant get an image to work with it... What exactly do you have to do‽ I added a resource png called Icon1 and tried this:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
<ribbon startFromScratch="false">
<tabs>
<tab id="TabToolss" label="Tools">
<group id="MyGroup" label="My Group" visible="true">
<button
id="Button1"
image="WordAddIn1.Properties.Resources.Icon1"
onAction="Button1_Click"
showImage="true"
/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>


No luck... not sure why. Even if I put the full file path in there it doesn't work.

I've never gotten it to work once, so maybe I'm just not doing it the way it was made to be done...

Answer

Very complicated solution... lucky to find it here

First add loadImage attribute to CustomUI tab,

Then to simplify things, add this internal class

internal class PictureConverter : AxHost
    {
        private PictureConverter() : base(String.Empty) { }

        static public stdole.IPictureDisp ImageToPictureDisp(Image image)
        {
            return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
        }

        static public stdole.IPictureDisp IconToPictureDisp(Icon icon)
        {
            return ImageToPictureDisp(icon.ToBitmap());
        }

        static public Image PictureDispToImage(stdole.IPictureDisp picture)
        {
            return GetPictureFromIPicture(picture);
        }
    }

Next add definition of loadImage function,

public IPictureDisp Ribbon_LoadImage(string imageName)
        {
            return PictureConverter.ImageToPictureDisp((Bitmap)Resources.ResourceManager.GetObject(imageName));
        }
Comments