我需要在收件箱的上下文菜单中添加一个按钮。我有这个工作正常。我需要弄清楚的是在事件处理程序的实现中如何确定点击了哪些项目/项目?
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事件处理程序中,我需要复制右键单击的特定项目,但我无法弄清楚如何判断单击了哪个项目。
答案 0 :(得分:1)
您可以在CommandBarButton
中使用tag属性答案 1 :(得分:1)
我已经编写了代码来解决你的问题,看看它:
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);
}