【问题标题】:How can I add the images to button using the ribbon xml?如何使用功能区 xml 将图像添加到按钮?
【发布时间】:2014-02-04 04:35:46
【问题描述】:

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

我尝试了将图像添加到功能区按钮的链接,但没有运气:-(。我正在为 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>

代码sn-p

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;
    }

请帮助我。 我正在为 Office 2010 使用 .net 4.0 c# VSTO excel 插件。

【问题讨论】:

    标签: c# .net vsto ribbon


    【解决方案1】:

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

    在 Ribbon.xml 中

    <button id="btnLogo" getImage="imageSuper_GetImage" size="large" />
    

    Ribbon.cs

    public Bitmap imageSuper_GetImage(IRibbonControl control)
            {
                return Resources.super_logo;
            }
    

    【讨论】:

    • 我的意思是你知道为什么会有 loadImage 吗?我永远无法让它发挥作用。我现在了解并使用 getImage。
    • 我觉得思路是你用loadImage只指定一次加载方式,用getImage指定任意数量的加载方式。
    • @Sanju Chris 是对的。 Chris 所引用的内容以及您似乎正在寻找的内容是 described here by MSDN with an example。希望这会有所帮助。
    • 但是如何将 super_logo 放入资源中?
    • @Daniel 在项目的 Properties 文件夹中双击 Resources.resx 文件。现在只需将图像拖到该窗口上即可。它会自动切换到该资源文件的图像视图并添加一个引用,如果项目还没有包含它,它还会将图像添加到资源文件夹中。
    【解决方案2】:

    您可以从 LoadImage 函数中获取图像。

    你需要这样写:

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
      loadImage="GetImage">
    
    public stdole.IPictureDisp GetImage(string imageName){
      return
        PictureConverter.IconToPictureDisp(Properties.Resources.MyIcon);
    }
    

    【讨论】:

      【解决方案3】:

      这是一篇旧帖子,但我想我会添加我的答案,以防有人仍在寻找示例(就像我一样)......

      在 Ribbon.xml 中,loadImage="GetImage" 引用 Ribbon.cs 中将从资源中获取图像的回调。在下面的示例中,我使用 image="Report_256x" 来触发回调。

      <?xml version="1.0" encoding="UTF-8"?>
      <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui" loadImage="GetImage">
          <ribbon>
              <tabs>
                  <tab idMso="TabMail">
                      <group id="group1" label="Priority Tracker">
                          <button id="btnWIPReport" onAction="btnWIPReport_Click" label="WIP Report" size="large" image="Report_256x"/>
                      </group>
                  </tab>
              </tabs>
          </ribbon>
      </customUI>
      

      我在示例中使用的回调如下所示...

      public System.Drawing.Image GetImage(string ImageName)
      {
          return (System.Drawing.Image)Properties.Resources.ResourceManager.GetObject(ImageName);
      }
      

      【讨论】:

      • 现在是否也可以在此处使用矢量图形(例如 *.svg)?默认的办公室图标似乎是矢量格式,但我没有找到任何信息如何在功能区中使用自定义矢量图标
      猜你喜欢
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多