C# VSTO给Excel添加右键菜单并添加点击的click事件

Excel.Application app;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    app = Globals.ThisAddIn.Application;
    Office.CommandBars bars = app.CommandBars;            
    Office.CommandBar bar = bars["cell"];  //获取右键菜单项
    bar.Reset();
    Office.CommandBarControls controls = bar.Controls;
    Office.CommandBarButton newControl =(Office.CommandBarButton)controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
    newControl.Caption = "My test";
    newControl.Click += NewControl_Click;
}

private void NewControl_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
    System.Windows.Forms.MessageBox.Show("Test");
}

猜你喜欢

转载自www.cnblogs.com/huang1314wei/p/11061295.html