前提:

软件模块里用 WindowsFormsHost 嵌入Office Word,可操作Word

功能;

Word表格内,鼠标右键,增加自定义的菜单项(如 ”测试“)

实现(粗暴上代码)》》

  • 设定菜单项属性内容

/// <summary>
/// 菜单项"测试"的Tag标记
/// </summary>
public static readonly string AddMenuTag = "MenuTest";/// <summary>
/// 菜单项"测试"的文字
/// </summary>
public static readonly string AddMenuCaption = "测试";/// <summary>
/// 菜单项"测试"所在NameLocal命令栏
/// a.Table Cells 表单元格
/// b.Table Text 表格文字
/// c.Table Headings 表格标题
/// </summary>
public static readonly List<string> AddMenuCommandBarNameLocals = new List<string>() { "Table Cells", "Table Text", "Table Headings" };
  • 菜单的初始化与清理时机

在打开Word的处理中,加入菜单项的初始化;关闭时清理菜单项

右键点击事件前设置菜单的Click事件,否则触发无效

/// <summary>
/// 打开Word
/// </summary>
private void OpenFile()
{//...Open File...//添加菜单项,bool-菜单项是否可用InitReportRightMenu(true);AppWord.WindowBeforeRightClick += AppWord_WindowBeforeRightClick;
}/// <summary>
/// Word右键点击前的事件处理
/// </summary>
/// <param name="Sel"></param>
/// <param name="Cancel"></param>
public void AppWord_WindowBeforeRightClick(Word.Selection Sel, ref bool Cancel)
{//检查菜单项是否已存在,不存在则再初始化添加一次//然后未菜单项注册点击后的处理事件(Handler)InitReportRightMenuHandler(true);
}/// <summary>
/// 关闭Word
/// </summary>
private void CloseFile()
{//清理Word右键菜单及事件(虽然微软有说会根据菜单字段自动清理,但出现过本地Word扔存在菜单的情况,建议手动处理一次)WordBll.RemoveCommandBarControl(WordDocument?.Document);if (AppWord != null){AppWord.WindowBeforeRightClick -= AppWord_WindowBeforeRightClick;}//...Close File...
}
  • 方法处理

以下为菜单初始化接口,实际Word操作的内容在"Word菜单控制"

点击右键"测试"后,实际执行方法为 AddMenuClick()

/// <summary>
/// 初始化报告右键菜单项
/// </summary>
/// <param name="enableStatus">是否启用</param>
public void InitReportRightMenu(bool enableStatus = true)
{//先清理菜单项WordBll.RemoveCommandBarControl(WordDocument?.Document);//依次为菜单不同的命令栏(不同位置触发右键,命名栏不同)添加右键菜单项foreach (var commandBarNameLocal in AddMenuCommandBarNameLocals){WordBll.AddCommandBarControl(WordDocument, commandBarNameLocal, AddMenuTag, AddMenuCaption, enableStatus);}
}/// <summary>
/// 初始化报告右键菜单项(含处理事件)
/// </summary>
/// <param name="enableStatus">是否启用</param>
public void InitReportRightMenuHandler(bool enableStatus = true)
{foreach (var commandBarNameLocal in AddMenuCommandBarNameLocals){WordBll.AddCommandBarControl(WordDocument, commandBarNameLocal, AddMenuTag, AddMenuCaption, AddMenuClick, enableStatus);}
}/// <summary>
/// 点击后的事件处理
/// </summary>
private void AddMenuClick(Microsoft.Office.Core.CommandBarButton ctrl, ref bool cancelDefault)
{//...TO DO...
}
  • Word菜单控制

/// <summary>
/// WordDocument清理菜单项
/// </summary>
/// <param name="wordDocument">WordDocument</param>
public static void RemoveCommandBarControl(Document document)
{if (document == null){return;}foreach (var commandBarNameLocal in AddMenuCommandBarNameLocals){//使用"FindControls"范围比较大//object oMissing = System.Reflection.Missing.Value;//var commandBarButtons = wordDocument.Document.Application.CommandBars.FindControls(//                       Microsoft.Office.Core.MsoControlType.msoControlButton, oMissing, controlTag, false);//if (commandBarButtons?.Count > 0)//{//    foreach (Microsoft.Office.Core.CommandBarButton btn in commandBarButtons)//    {//        btn.Delete(true);//    }//}//找到指定命名栏对应的菜单项,匹配Tag找到指定菜单项var commandBar = document.Application.CommandBars[commandBarNameLocal];foreach (Microsoft.Office.Core.CommandBarControl tempContrl in commandBar.Controls){if (tempContrl.Tag == WordHelper.AddMenuTag){tempContrl.Delete();}}}
}/// <summary>
/// WordDocument添加菜单项
/// </summary>
/// <param name="wordDocument">WordDocument</param>
/// <param name="commandBarNameLocal">NameLocal命令栏的名称</param>
/// <param name="controlTag">添加项的Tag标记</param>
/// <param name="controlCaption">添加项的展示文字</param>
/// <param name="enableStatus">是否启用</param>
public static void AddCommandBarControl(WordDocument wordDocument, string commandBarNameLocal, string controlTag, string controlCaption, bool enableStatus = true)
{try{if (wordDocument == null){return;}//查找菜单的命名栏分组//foreach (Microsoft.Office.Core.CommandBar item in wordDocument.Document.Application.CommandBars)//{//    Console.WriteLine($"{item.NameLocal}{item.Name}");//}object oMissing = Missing.Value;var commandBar = wordDocument.Document.Application.CommandBars[commandBarNameLocal];//虽然Temporary属性true系统在退出时自动删除,但并不保险请合适时机调用清理var newCommandBarButton = (Microsoft.Office.Core.CommandBarButton)commandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,oMissing, oMissing, oMissing, true);newCommandBarButton.BeginGroup = true;//TagnewCommandBarButton.Tag = controlTag;//文字newCommandBarButton.Caption = controlCaption;//启用状态newCommandBarButton.Enabled = enableStatus;}catch (Exception ex){Log4NetHelper.Error("WordDocument添加菜单项 出错", ex);}
}/// <summary>
/// WordDocument添加菜单项
/// </summary>
/// <param name="wordDocument">WordDocument</param>
/// <param name="commandBarNameLocal">NameLocal命令栏的名称</param>
/// <param name="controlTag">添加项的Tag标记</param>
/// <param name="controlCaption">添加项的展示文字</param>
/// <param name="clickHandler">添加项的Click事件</param>
/// <param name="enableStatus">是否启用</param>
public static void AddCommandBarControl(WordDocument wordDocument, string commandBarNameLocal, string controlTag,string controlCaption, _CommandBarButtonEvents_ClickEventHandler clickHandler,bool enableStatus = true)
{if (wordDocument == null){return;}object oMissing = Missing.Value;Microsoft.Office.Core.CommandBarButton commandBarButton = null;var commandBar = wordDocument.Document.Application.CommandBars[commandBarNameLocal];foreach (Microsoft.Office.Core.CommandBarControl tempContrl in commandBar.Controls){//找到同Tag、同Caption的菜单项,即表示存在指定菜单项if (tempContrl.Tag == controlTag && tempContrl.Caption == controlCaption){commandBarButton = tempContrl as Microsoft.Office.Core.CommandBarButton;break;}}if (commandBarButton != null){//每次Click事件都需要右键点击事件前重新绑定commandBarButton.Click -= clickHandler;commandBarButton.Click += clickHandler;}else{var newCommandBarButton = (Microsoft.Office.Core.CommandBarButton)commandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,oMissing, oMissing, oMissing, true);newCommandBarButton.BeginGroup = true;newCommandBarButton.Tag = controlTag;newCommandBarButton.Caption = controlCaption;newCommandBarButton.Enabled = enableStatus;newCommandBarButton.Click += clickHandler;}
}

以上~OVER~注释清晰~ 码农很累的,尤其是很菜鸡的码农 ~

C# WPF 为Word右键添加自定义菜单项相关推荐

  1. 清理桌面右键新建菜单项

    目录 问题 条件 方法 问题 电脑桌面空白处"鼠标右键→新建"菜单项内容太杂,打算清理一下. 条件 Windows 7. 方法 以删除"新建docx文件"菜单项 ...

  2. 【Windows】快捷添加鼠标右键的菜单项

    场景 添加步骤 场景 将下载的免安装绿色软件添加快捷打开方式: 将已安装的程序添加快捷打开方式. 添加步骤 打开注册表编辑器(CMD->regedit): 找到如下目录: 在shell文件夹下新 ...

  3. 为Windows右键新建菜单添加菜单项

    [标题]:为Windows右键新建菜单添加菜单项 [时间]:2009-3-4 [摘要]:给新建菜单添加或删除一个菜单项,或者想可以新建自定义后缀文件类型的文件,本文是一个参考.如果"新建&q ...

  4. UltraEdit添加删除右键菜单项

    有时UltraEdit没法集成在资源管理器的右键菜单中,要怎么办呢? 在uedit32.exe这个可执行文件的目录中新建一个文件,例如叫做1添加删除右键菜单项.bat 1添加删除右键菜单项.bat 文 ...

  5. WPF界面开发第三方控件入门指南——菜单项

    下载DevExpress v20.2完整版 DevExpress技术交流群3:700924826      欢迎一起进群讨论 DevExpress WPF Subscription拥有120+个控件和 ...

  6. WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法

    WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法 原文:WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法 问题描 ...

  7. MSDN Visual系列:创建Feature扩展SharePoint列表项或文档的操作菜单项

    原文:http://msdn2.microsoft.com/en-us/library/bb418731.aspx 在SharePoint中我们可以通过创建一个包含CustomAction元素定义的F ...

  8. joomla添加html,如何将自定义html代码添加到Joomla 2.5菜单项?

    这里是什么的Joomla提供了这样的情况: 在菜单管理 - >菜单项编辑 编辑每个菜单项,您将看到"链接类型选项"在右栏的部分. 见截图: 正如你在截图中看到,这组设置包含如 ...

  9. 如何在系统菜单中添加一个自己的菜单项

    CSDN地址: http://topic.csdn.net/u/20080722/18/8517e419-7788-4d1c-9029-7a806f5da46c.html 全文: 如题,目前我要做的就 ...

最新文章

  1. 二叉树:二叉搜索树的创建和插入
  2. 语音识别框架最新进展——深度全序列卷积神经网络登场
  3. freemarker.properties的属性文件的配置说明
  4. 李彦宏最新演讲:移动互联网的时代已经结束了
  5. 乌班图系统的MySQL_乌班图系统mysql主从备份
  6. php 随机颜色,php生成随机颜色的代码实例
  7. 搬家,又一次和过往告别
  8. linux ps pstree pstack命令
  9. 拳王公社:网络引流的“4大核心秘诀“,让客户源源不断地加你
  10. 带有en的单词有哪些_英语前缀大全en:开头是EN的单词有哪些
  11. 信息短信服务器发送失败怎么办,短信发送失败如何设置
  12. 2020CADCG专题报告笔记 Jittor计图 深度学习框架
  13. 自定义View——幸运转盘
  14. 38译码器真值表讲解_74138译码器真值表及引脚图功能
  15. NEC the WISE点亮的不仅仅是新零售
  16. MUI框架TAB切换
  17. 2021年Bootstrap实用手册和最强总结以及工具
  18. 从魔兽看四种设计模式(转载)
  19. 智能合约和去中心化应用DAPP
  20. windows自带的待办事项工具在哪?

热门文章

  1. [Excel VBA]判斷英文字母是否為大寫
  2. Chrome浏览器新版本解决CROS的技巧
  3. 通俗简单讲解贝叶斯原理,并python实现贝叶斯分类代码
  4. concurrent.futures --- 启动并行任务(线程池)
  5. awk显示指定行到末尾行之间小技巧
  6. eclips安装svn插件方法
  7. 美欧股市周一综述:美国股市走低,欧洲股市上涨
  8. HTC手机官解、S-ON/S-OFF与超级CID的关系
  9. 学生报名太火热,黑马大门要被挤掉了?
  10. 知识补充2:Elasticsearch的基本使用(Windows+Python)