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

发布于 2025-01-01 16:41:37 字数 1033 浏览 4 评论 0原文

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

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

    }

对于字符串参数,我使用了所有 ComandBar 名称:

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

它所做的就是在 Addins 选项卡的功能区中添加按钮。右键菜单中没有添加任何按钮。您知道添加右键菜单的另一种方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱咩 2025-01-08 16:41:37

MS Project 中的上下文菜单
看一下这个链接,看看它是否有帮助

这是另一个也处理上下文菜单的链接
Office 项目添加上下文菜单

此链接将解释如何在右键单击鼠标时创建上下文菜单
用户右键单击时创建上下文菜单鼠标

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

雪化雨蝶 2025-01-08 16:41:37

您需要使用

nofollow noreferrer">功能区 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;
        }
}

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;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文