C# MS Word加载项:右键单击处理程序
我正在为MS Word 2010开发一个外接程序,我想在右键单击菜单中添加两个菜单项(仅当选择某些文本时)。我已经看到了几个添加项目的示例,但找不到如何有条件地添加项目。 简言之,我想覆盖类似OnRightClick处理程序的内容。C# MS Word加载项:右键单击处理程序,c#,ms-word,add-in,C#,Ms Word,Add In,我正在为MS Word 2010开发一个外接程序,我想在右键单击菜单中添加两个菜单项(仅当选择某些文本时)。我已经看到了几个添加项目的示例,但找不到如何有条件地添加项目。 简言之,我想覆盖类似OnRightClick处理程序的内容。 提前感谢。这非常简单,您需要在右键单击事件之前处理窗口。在事件内部找到所需的命令栏和specfic控件,并处理可见或启用属性 在下面的示例中,我切换基于选择在文本命令栏上创建的自定义按钮的Visible属性(如果选择包含“C#”,则隐藏按钮,否则显示按钮) 这很简单
提前感谢。这非常简单,您需要在右键单击事件之前处理
窗口。在事件内部找到所需的命令栏和specfic控件,并处理可见
或启用
属性
在下面的示例中,我切换基于选择在文本命令栏上创建的自定义按钮的Visible
属性(如果选择包含“C#”,则隐藏按钮,否则显示按钮)
这很简单,我想知道在谷歌搜索了这么多之后我怎么找不到这个处理程序:)谢谢ThoughtSetting应用程序。应用程序会导致一个错误,因为它在Addin_启动期间还没有打开。
//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;
}