VSTO Outlook 2007 加载项上下文菜单 CommandBarButton 单击事件

发布于 2024-09-19 04:05:40 字数 866 浏览 6 评论 0原文

我需要在收件箱的上下文菜单中添加一个按钮。我这个工作正常。我需要弄清楚的是,在事件处理程序的实现中,如何确定单击了哪个项目?


private void AddIn_Startup(object sender, EventArgs e)
{
   Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
}

private void Application_ItemContextMenuDisplay(CommandBar commandBar, Selection selection)
{
   commandBar.Controls[1].BeginGroup = true; // add seperator before first menu

   var cmdButtonCopy = (CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, 1, Missing.Value, 1, Missing.Value);
   cmdButtonCopy.Caption = "&Copy Message";
   cmdButtonCopy.Click += cmdButtonCopy_Click;
}

private void cmdButtonCopy_Click(CommandBarButton ctrl, ref bool canceldefault)
{
     // no sender/event args to determine which item was clicked ...
}

在 cmdButtonCopy_Click 事件处理程序中,我需要复制右键单击的特定项目,但我不知道如何判断单击了哪个项目。

I need to add a button to the context menu of the inbox. I have this working fine. What I need to figure out is in the implementation of the event handler how do I determine which item/items was clicked?


private void AddIn_Startup(object sender, EventArgs e)
{
   Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
}

private void Application_ItemContextMenuDisplay(CommandBar commandBar, Selection selection)
{
   commandBar.Controls[1].BeginGroup = true; // add seperator before first menu

   var cmdButtonCopy = (CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, 1, Missing.Value, 1, Missing.Value);
   cmdButtonCopy.Caption = "&Copy Message";
   cmdButtonCopy.Click += cmdButtonCopy_Click;
}

private void cmdButtonCopy_Click(CommandBarButton ctrl, ref bool canceldefault)
{
     // no sender/event args to determine which item was clicked ...
}

In the cmdButtonCopy_Click event handler I need to copy the specific item that was right-clicked but I can't figure out how to tell which item was clicked.

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

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

发布评论

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

评论(2

你与昨日 2024-09-26 04:05:40

您可以在 CommandBarButton 内使用 tag 属性

You can use tag property inside the CommandBarButton

紫竹語嫣☆ 2024-09-26 04:05:40

我已经写了代码来解决您的问题,请看一下:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Application.ItemContextMenuDisplay += new ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
    }

    void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection)
    {
        CommandBarButton mycmdbarbtn = (CommandBarButton)CommandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, 1,true);
        mycmdbarbtn.Caption = "Test Button";           
        mycmdbarbtn.Click += new _CommandBarButtonEvents_ClickEventHandler(mycmdbarbtn_Click);           
        mailitm=Selection.Application.ActiveExplorer().Selection[1]; // to get the currently selected mailitem.
    }

    void mycmdbarbtn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
    {
        MessageBox.Show("The subject of the clicked mail is " + mailitm.Subject);
    }

I have written the code to solve your issue have a look at it:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Application.ItemContextMenuDisplay += new ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
    }

    void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection)
    {
        CommandBarButton mycmdbarbtn = (CommandBarButton)CommandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, 1,true);
        mycmdbarbtn.Caption = "Test Button";           
        mycmdbarbtn.Click += new _CommandBarButtonEvents_ClickEventHandler(mycmdbarbtn_Click);           
        mailitm=Selection.Application.ActiveExplorer().Selection[1]; // to get the currently selected mailitem.
    }

    void mycmdbarbtn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
    {
        MessageBox.Show("The subject of the clicked mail is " + mailitm.Subject);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文