简体   繁体   English

如何在MS Project的右键菜单中添加菜单项?

[英]How to add menu item to right click menu in MS Project?

I am developing an add-in for MS Project in Visual Studio and I need a custom menu item in the right click menu . 我正在Visual Studio开发用于MS Project的加载项,并且在right click menu需要自定义菜单项。 This will modify task data. 这将修改任务数据。 I am using the following code to add a item: 我正在使用以下代码添加项目:

 private void AddMenuItem(String param)
    {
        Office.MsoControlType menuItem =
            Office.MsoControlType.msoControlButton;

        btn_editor =
            (Office.CommandBarButton)app.CommandBars[param].Controls.Add
            (menuItem, missing, missing, 1, true);

        btn_editor.Style = Office.MsoButtonStyle.msoButtonCaption;
        btn_editor.Caption = "My Menu Item";
        btn_editor.Tag = "MyMenuItem";

        btn_editor.Click +=
            new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
                (editor_Click);

    }

For String param I have used all the ComandBar names: 对于String参数,我使用了所有ComandBar名称:

 CommandBars commandBars = (CommandBars)app.CommandBars;
  foreach (CommandBar cbar in commandBars)
        {
                AddMenuItem(cbar.Name);
        }

All it did, was to add the button in the Ribbon in Addins Tab. 它所做的就是在“加载项”选项卡的“功能区”中添加按钮。 No button was added in the right click menu. 右键单击菜单中未添加任何按钮。 Do you know another way to add in right click menu? 您知道在右键菜单中添加的另一种方法吗?

Context Menus in MS Project Take a look at this link to see if it will help MS Project中的上下文菜单查看此链接以查看是否有帮助

Here is another one that deals with Context Menus as well Office Project adding Context Menu 这是另一个处理上下文菜单以及Office Project添加上下文菜单的方法

This link will explain how to get at creating a Context Menu when you Right-Click the Mouse Creating a Context Menu when user Right-Clicks the Mouse 此链接将说明如何在右键单击鼠标时创建上下文菜单,而在用户右键单击鼠标时创建上下文菜单。

You'll need to use the Ribbon XML API , this is an example for your case 您将需要使用Ribbon XML API ,这是您的案例的示例

XML snippet XML片段

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
   <contextMenus>
      <contextMenu idMso="ContextMenuText">
         <button id="MyMenuItem" label="My Menu Item" onAction="Button_Click" />
      </contextMenu>
   </contextMenus>
</customUI>

Ribbon Code 功能区代码

public void Button_Click(Microsoft.Office.Core.IRibbonControl ctrl)
{
        switch (ctrl.Id)
        {
            case "MyMenuItem": System.Windows.Forms.MessageBox.Show("MyMenuItem"); break;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM