首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MS Word加载项: RIght单击处理程序

MS Word加载项: RIght单击处理程序
EN

Stack Overflow用户
提问于 2012-08-05 20:20:26
回答 1查看 4.1K关注 0票数 4

我正在为MS Word 2010开发一个插件,我想添加几个菜单项到右击菜单(只有当一些文本被选中时)。我已经看过几个添加项目的示例,但找不到如何有条件地添加项目。简而言之,我想覆盖像OnRightClick处理程序这样的东西。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-29 18:11:27

这非常简单,您需要处理WindowBeforeRightClick事件。在事件内部,找到所需的命令栏和特定的控件,并处理VisibleEnabled属性。

在下面的示例中,我根据选择切换在文本命令栏上创建的自定义按钮的Visible属性(如果选择包含"C#“,则隐藏该按钮,否则显示它)

代码语言:javascript
复制
    //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;
    }
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11816358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档