RibbonX 动态生成ComboBox和DropDown的Item

黄博艺
2023-12-01

先让我们来看一下xml文件的设定:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" 
onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab id="TabCustom" label="ExcelAddIn12Tab">
        <group id="GroupExcelAddIn12"
               label="ExcelAddIn12">
          <comboBox id="ComboBoxSample" getItemCount ="ComboBoxGetItemCount" 
getItemLabel ="ComboBoxGetItemLabel" getItemID="ComboBoxGetItemID" 
getItemScreentip="ComboBoxGetItemScreenTip"/>
          <dropDown id="DropDownSample" getItemCount="DropDownGetItemCount" 
getItemLabel ="DropDownGetItemLabel" getItemID ="DropDownGetItemID" 
getItemScreentip ="DropDowGetItemScreenTip"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

这个xml文件设定了一个ID是ComboBoxSample的ComboBox和一个DropDownSample的DropDown.并且为这两个控件设定了一些 内置调用方法(这是我自己给起的说法,因为这些方法不是被控件以外的程序调用,而是控件自身调用的)。由于ComboBox和DropDown本来很像所以其内置方法也一样。其中getItemCount内置方法是其他内置方法实现的先决条件。没有这个方法其他内置方法将会出错。

另外getItemID内置方法用处不大,致少在UI显示上我们不能看到它起作用(Debug时你会发现此方法会先于getItemLabel被调用(调用顺序getItemCount->getItemID->getItemLabel->getItemScreenTip)。

下面是Ribbon1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or 
// ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility 
//CreateRibbonExtensibilityObject()
//  {
//      return new Ribbon1();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to 
// handle user actions, such as clicking a button. Note: if you have exported 
// this Ribbon from the Ribbon designer, move your code from the event handlers 
// to the callback methods and modify the code to work with the Ribbon 
// extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify 
// the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio 
// Tools for Office Help.


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

        public Ribbon1()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("ExcelAddIn12.Ribbon1.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion

        #region MyCode
        public int ComboBoxGetItemCount(Office.IRibbonControl Control)
        {
            return 5;
        }
        public string ComboBoxGetItemLabel(Office.IRibbonControl Control,
            int Index)
        {
            return "ComboBoxItem" + Index.ToString();
        }
        public string ComboBoxGetItemID(Office.IRibbonControl Control, int Index)
        {
            return "cb" + Index.ToString();
        }
        public int DropDownGetItemCount(Office.IRibbonControl Control)
        {
            return 3;
        }
        public string DropDownGetItemLabel(Office.IRibbonControl Control,
            int Index)
        {
            return "DropDownItem" + Index.ToString();
        }
        public string DropDownGetItemID(Office.IRibbonControl Control, int Index)
        {
            return "dd" + Index.ToString();
        }
        public string ComboBoxGetItemScreenTip(Office.IRibbonControl Control,
            int Index)
        {
            return string.Format("This is \"{0}\", its id is {1}", "ComboBoxItem"
+ Index.ToString(), "cb" + Index.ToString());
        }
        public string DropDowGetItemScreenTip(Office.IRibbonControl Control,
            int Index)
        {
            return string.Format("This is \"{0}\", its id is {1}", "DropDownItem"
+ Index.ToString(), "dd" + Index.ToString());
        }
        #endregion
    }
}

相关资源: http://download.csdn.net/detail/tx_officedev/3923959

转载于:https://www.cnblogs.com/mksword/archive/2011/12/15/3934138.html

 类似资料: