c# (outlook插件)文件夹上的上下文菜单

本文关键字:上下文 菜单 文件夹 outlook 插件 | 更新日期: 2023-09-27 17:49:37

在我的VSTO outlook插件中,我试图放置一个按钮,当我右键单击文件夹时将显示。在我的启动函数中,我有这个:

Outlook.Application myApp = new Outlook.ApplicationClass();
myApp.FolderContextMenuDisplay += new ApplicationEvents_11_FolderContextMenuDisplayEventHandler(myApp_FolderContextMenuDisplay);

那么我就有了处理程序。

void myApp_FolderContextMenuDisplay(CommandBar commandBar, MAPIFolder Folder)
{
    var contextButton = commandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true) as CommandBarButton;
    contextButton.Visible = true;
    contextButton.Caption = "some caption...";
    contextButton.Click += new _CommandBarButtonEvents_ClickEventHandler(contextButton_Click);
}

最后是click....

的处理程序
void contextButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
    //stuff here
}

我的问题是如何将MAPIFolder FoldermyApp_FolderContextMenuDisplay发送到contextButton_Click ?

(如果可以用其他方法,我也愿意听取建议)

c# (outlook插件)文件夹上的上下文菜单

最简单的方法就是使用闭包,例如:

// where Folder is a local variable in scope, such as code in post
contextButton.Click += (CommandBarButton ctrl, ref bool cancel) => {
   DoReallStuff(ctrl, Folder, ref cancel);
};

如果需要,请确保清理事件。需要注意的一件事是,文件夹的RCW现在可能有一个"延长的生命周期",因为闭包可能使其存活的时间比以前更长(但是对于OOM来说,在不需要时手动释放RCW是非常重要的)。

快乐编码。