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

时间:2012-02-03 14:16:31

标签: c# visual-studio menu add-in right-click

我正在MS Project中为Visual Studio开发一个加载项,我需要right click menu中的自定义菜单项。这将修改任务数据。我使用以下代码添加项目:

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

    }

对于String param,我使用了所有ComandBar名称:

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

所有这一切,都是在Addins选项卡的Ribbon中添加按钮。右键菜单中未添加任何按钮。您是否知道在右键菜单中添加其他方式?

2 个答案:

答案 0 :(得分:1)

Context Menus in MS Project 看一下这个链接,看它是否有帮助

这是另一个处理Context Menus的问题 Office Project adding Context Menu

此链接将说明右键单击鼠标时如何创建上下文菜单 Creating a Context Menu when user Right-Clicks the Mouse

答案 1 :(得分:0)

您需要使用Ribbon XML API,这是您的案例的示例

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>

功能区代码

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