利用Custom Ribbon XML 重写Office内置按钮

本文详细介绍了如何使用OfficeApplicationlevelAddIn创建Powerpoint 2010的自定义功能区(Ribbon),并实现了特定的回调函数来控制功能按钮的行为。通过XML配置文件定义了命令和功能区布局,展示了如何在Powerpoint应用内扩展功能,提供了一个实用的开发示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.  创建Office Application level AddIn(eg. Powerpoint 2010 AddIn)

2. 添加Ribbon.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <commands>
    <command idMso="FileOpen" getEnabled="rxshared_setDisabled"/>
    <command idMso="FileClose" getEnabled="rxshared_setDisabled"/>
    <command idMso="FileExit" getEnabled="rxshared_setDisabled"/>
    <command idMso="FileSave" onAction="rxFileSave_repurpose"/>
  </commands>
  <!--<ribbon>
    <tabs>
      <tab idMso="TabAddIns">
        <group id="MyGroup"
               label="My Test Group">
        </group>
      </tab>
    </tabs>
  </ribbon>-->
</customUI>

3. 添加Callback函数到Ribbon1.cs 中

        private bool repurposing = true;
        // disable the command
        public bool rxshared_setDisabled(IRibbonControl control)
        {
            return false;
        }
        // repurpose the command
        public void rxFileSave_repurpose(IRibbonControl control, bool cancelDefault)
        {
            if (repurposing)
            {
                MessageBox.Show("The Save button has been temporarily repurposed.");
                cancelDefault = false;
            }
            else
            {
                cancelDefault = false;
            }
        }

运行效果


public class MyAddin : IDTExtensibility2, IRibbonExtensibility { public static Application wpp; public static Application app = null; // 插件加载时触发 public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { wpp = (Application)application; wpp.DocumentOpen += OnDocumentOpen; app = wpp as Word.Application; } // 文档打开时插入“Hello World” private void OnDocumentOpen(Document doc) { //doc.Content.Text = "Hello World from COM Add-in!"; } // 其他接口方法(保持空实现) public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { } public void OnAddInsUpdate(ref Array custom) { } public void OnStartupComplete(ref Array custom) { } public void OnBeginShutdown(ref Array custom) { } public string GetCustomUI(string RibbonID) { return @" <customUI xmlns='http://schemas.microsoft.com/office/2019/07/customui' onLoad='Ribbon_Load'> <ribbon> <tabs> <tab id='MainTab' label='AI助手'> <!-- 新写功能 --> <group id='GroupNew' label='新创作'> <button id='btnNewWrite' label='新写' size='large' getImage='GetButtonImage' onAction='OnButtonClick' getEnabled='GetButtonEnabled' showItemLabel='true' itemWidth='200' itemHeight='80' screentip='新建文档' supertip='创建新的智能文档'/> <!-- 内容续写 --> <button id='btnContinueWrite' label='续写' size='large' getImage='GetButtonImage' onAction='OnButtonClick' getEnabled='GetButtonEnabled' screentip='继续写作' supertip='根据现有内容继续扩展写作'/> <!-- 内容扩展 --> <button id='btnExpandWrite' label='扩写' size='large' getImage='GetButtonImage' onAction='OnButtonClick' getEnabled='GetButtonEnabled' screentip='扩展内容' supertip='对现有内容进行详细扩展'/> <!-- 优化改写 --> <button id='btnRewrite' label='重写' 这是我现在的代码.我要在点击"新写"按钮时,在文档右侧打开一个窗格
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值