/// <summary> 初始化右键菜单 /// </summary> private void initRightBtn() { RemoveRightBtns(); Office.CommandBarControls siteBtns = Application.CommandBars.FindControls(Office.MsoControlType.msoControlButton, missing, "BookMarkAddin", false); if (siteBtns != null) { foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in siteBtns) { //如果已经存在就删除 if (temp_contrl.Tag == "BookMarkAddin" || temp_contrl.Tag == "checkOutline") { temp_contrl.Delete(); } } } // 添加右键按钮 Office.CommandBarButton addBtn = (Office.CommandBarButton)Application.CommandBars["Text"].Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, false); addBtn.BeginGroup = true; addBtn.Tag = "BookMarkAddin"; addBtn.Caption = "查看概要"; addBtn.Enabled = true; this.Application.WindowBeforeRightClick += new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(Application_WindowBeforeAddBtnClick); //// 添加右键按钮 //Office.CommandBarButton checkBtn = (Office.CommandBarButton)Application.CommandBars["Text"].Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, false); //checkBtn.BeginGroup = true; //checkBtn.Tag = "checkOutline"; //checkBtn.Caption = "查看概要"; //checkBtn.Enabled = true; //checkBtn.Click += checkBtn_Click; }
private void RemoveRightBtns() { Office.CommandBarControls siteBtns = Application.CommandBars.FindControls(Office.MsoControlType.msoControlButton, missing, "BookMarkAddin", false); if (siteBtns == null) { return; } foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in siteBtns) { //如果已经存在就删除 if (temp_contrl.Tag == "BookMarkAddin" || temp_contrl.Tag == "checkOutline") { temp_contrl.Delete(); } } }
private void ThisAddIn_Startup(object sender, System.EventArgs e) { // 初始化右侧解析框 UCForRichText richTextBox = new UCForRichText(); _MyCustomTaskPane = this.CustomTaskPanes.Add(richTextBox, "解析结果"); _MyCustomTaskPane.Width = 800; _MyCustomTaskPane.Visible = false; //获取右键菜单对象mzBar Office.CommandBar mzBar = Application.CommandBars["Text"]; //重置菜单 mzBar.Reset(); //获取右键菜单的控制对象 Office.CommandBarControls bars = mzBar.Controls; /*已被mzBar.Reset();替代,用于除去其余标签为"autoAnswer"的菜单项 foreach (Office.CommandBarControl temp_contrl in bars) { string t = temp_contrl.Tag.Trim(); if (t.Equals("autoAnswer")) { temp_contrl.Delete(); } } */ //在右键菜单列表添加新的菜单项 Office.CommandBarControl comControl = bars.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true); //添加自己的菜单项 //将该菜单项转为按钮可点击模式 Office.CommandBarButton comButton = comControl as Office.CommandBarButton; if (comControl != null) { comButton.Tag = "autoAnswer";//添加标签"autoAnswer" comButton.Caption = "选择答案";//设置文本 comButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption; comButton.Enabled = false;//设置不可点击 //name = comButton.accName; //comButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(_RightBtn_Click); } Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument); vstoDoc.BeforeDoubleClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeDoubleClick); this.Application.WindowBeforeRightClick += new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(Application_WindowBeforeRightClick); }