带有色带的Visual Studio图像(XML)

时间:2011-08-21 05:12:22

标签: c# .net xml visual-studio ribbon

我正在制作Office 2007插件,我正在尝试使用XML而不是可视化设计器来自定义功能区但由于某种原因我无法获得使用它的图像...您究竟需要做什么‽我添加了一个名为Icon1的资源png并尝试了这个:

<?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>

没有运气......不确定原因。即使我把完整的文件路径放在那里它也不起作用。

我从来没有让它工作一次,所以也许我只是不按照它的方式去做...

2 个答案:

答案 0 :(得分:10)

非常复杂的解决方案......很幸运能找到它here

首先将loadImage属性添加到CustomUI选项卡

然后为简化事情,添加此内部类

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

下一步添加loadImage函数的定义,

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

答案 1 :(得分:0)

如果您执行以下代码,

会更容易:

<?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" size="normal" getImage="Icon1"
        onAction="Button1_Click"
        showImage="true"/>
    </group>
  </tab>
 </tabs>
</ribbon>

功能区cs中的

namespace Add_in
{
[ComVisible(true)]
public class MyRibbon : Office.IRibbonExtensibility
{
    private Office.IRibbonUI ribbon;

    public MyRibbon()
    {

    }

    public Bitmap Icon1(Office.IRibbonControl control)
    {
        return (Bitmap)Properties.Resources.ResourceManager.GetObject("Icon1");
    }