如何使用功能区xml将图像添加到按钮?

roo*_*i n 9 .net c# vsto ribbon

如何将自定义图像添加到选项卡和上下文菜单中的功能区按钮.

我尝试了添加图像到功能区按钮的链接,但没有运气:-(.我正在设计Excel的插件.我在标题中添加了这个.

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"  onLoad="Ribbon_Load"   loadImage="Ribbon_LoadImage"> 
    <button id="btn2d" keytip="L" screentip="2D Visualization" supertip="2D Part Visualization" label="2D" size="large"/>
    <contextMenu idMso="ContextMenuCell">
    <button id="btn1" label="my label"/>
    </customUI>
Run Code Online (Sandbox Code Playgroud)

代码段

public Bitmap Ribbon_LoadImage(IRibbonControl control)
    {
        switch (control.Id)
        {
            case "btn2": return new Bitmap(Properties.Resources.btn1);
            case "btn3": return new Bitmap(Properties.Resources.btn2);
            case "btn4": return new Bitmap(Properties.Resources.btn3);
            case "btn5": return new Bitmap(Properties.Resources.Filter);
            case "btnOpt6": return new Bitmap(Properties.Resources.Settings);
            case "btnInform7": return new Bitmap(Properties.Resources.Vis);
            case "btnHelpPage": return new Bitmap(Properties.Resources.Help);
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

请帮帮我.我正在使用.net 4.0 c#VSTO excel addin for Office 2010.

Kir*_*iru 27

您必须getImage为每个按钮使用属性,并且回调应返回位图.

在Ribbon.xml中

<button id="btnLogo" getImage="imageSuper_GetImage" size="large" />
Run Code Online (Sandbox Code Playgroud)

Ribbon.cs

public Bitmap imageSuper_GetImage(IRibbonControl control)
        {
            return Resources.super_logo;
        }
Run Code Online (Sandbox Code Playgroud)

  • @Daniel 在项目的 Properties 文件夹中双击 Resources.resx 文件。现在只需将图像拖到该窗口上即可。它将自动切换到该资源文件的图像视图并添加引用,如果项目尚不包含图像,它还会将图像添加到资源文件夹中。 (3认同)
  • 我认为这个想法是你使用`loadImage`只指定一次加载方法,并使用`getImage`来指定任意数量的加载方法. (2认同)