MS Word加载项:右键点击处理器 [英] MS Word Add-in: RIght click handler
问题描述
我开发的MS Word 2010的外接,我想了几个菜单项目添加到右键菜单(仅当选择一些文本)。我见过一对夫妇的例子来补充项目,但怎么也找不到有条件地添加项目。
总之我想重写像OnRightClick处理程序。
先谢谢了。
I am developing an Add-in for MS Word 2010 and I want to add a couple of menu items to the right click menu (only when some text is selected). I have seen a couple of examples to add items but couldn't find how to add items conditionally. In short I want to override something like OnRightClick handler. Thanks in advance.
推荐答案
这是很简单的,你需要处理 WindowBeforeRightClick
事件。该事件中找到所需要的命令栏和specfic控制和处理无论是可见
或已启用
属性。
This is quite simple, you need to handle the WindowBeforeRightClick
event. Inside the event locate the required command bar and the specfic control and handle either the Visible
or the Enabled
property.
在下面的例子中我切换文本命令栏上创建一个自定义按钮的基础上,选择可见
财产(如果选择包含C#隐藏按钮,否则显示它)
In the example below I toggle the Visible
property of a custom button created on the Text command bar based on the selection(If selection contains "C#" hide the button otherwise show it)
//using Word = Microsoft.Office.Interop.Word;
//using Office = Microsoft.Office.Core;
Word.Application application;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
application = this.Application;
application.WindowBeforeRightClick +=
new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);
application.CustomizationContext = application.ActiveDocument;
Office.CommandBar commandBar = application.CommandBars["Text"];
Office.CommandBarButton button = (Office.CommandBarButton)commandBar.Controls.Add(
Office.MsoControlType.msoControlButton);
button.accName = "My Custom Button";
button.Caption = "My Custom Button";
}
public void application_WindowBeforeRightClick(Word.Selection selection, ref bool Cancel)
{
if (selection != null && !String.IsNullOrEmpty(selection.Text))
{
string selectionText = selection.Text;
if (selectionText.Contains("C#"))
SetCommandVisibility("My Custom Button", false);
else
SetCommandVisibility("My Custom Button", true);
}
}
private void SetCommandVisibility(string name, bool visible)
{
application.CustomizationContext = application.ActiveDocument;
Office.CommandBar commandBar = application.CommandBars["Text"];
commandBar.Controls[name].Visible = visible;
}
这篇关于MS Word加载项:右键点击处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!