In this example I will quickly show how to set a custom image that is
part of the "External Library" assembly embedded resources. This is
useful for people who want to control the ribbon in a compiled
assembly rather than in the .dna file.
Please note that the using the .dna file for menu items (i.e. ribbon
or command bars) is by far the easiest and simplest way of doing
things.
Assume you have an embedded image resource (m.png) in your "External
Library" assembly. To set this image on a ribbon button you need to
hook onto the getImage callback attribute for that ribbon control
which returns a IPictureDisp interface. Govert exposes the
IPictureDisp interface through ExcelDna.Integeration which helps a lot
for this example.
using...
Make sure that you have all the necessary references.
[ComVisible(true)]
public class MyRibbon : ExcelRibbon
{
public override string GetCustomUI(string uiName)
{
return
@"
<customUI xmlns='
http://schemas.microsoft.com/office/2006/01/
customui'>
<ribbon>
<tabs>
<tab idMso='TabAddIns'>
<group id='group1' label='Group1'>
<button id='button1' label='Button1'
getImage='GetButtonImage' showImage='true' />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
";
}
public IPictureDisp GetThisImage(IRibbonControl control)
{
System.Drawing.Image i;
i = ClassLibrary1.Properties.Resources.m;
// or you could also do
// i = new System.Drawing.Bitmap("C:\\temp\\m.png");
return AxHostConverter.ImageToPictureDisp(i);
}
}
internal class AxHostConverter : System.Windows.Forms.AxHost
{
private AxHostConverter() : base("") { }
static public IPictureDisp ImageToPictureDisp(System.Drawing.Image
image)
{
return (IPictureDisp)GetIPictureDispFromPicture(image);
}
}
See the blog entry by Andrew Whitechapel for more info on how to
convert System.Drawing.Image to IPictureDisp.
I hope this safes someone a bit of time.
- Gideon