简体   繁体   中英

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 . 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:

 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

Here is another one that deals with Context Menus as well Office Project adding Context Menu

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

XML snippet

<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;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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