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

108 篇文章 8 订阅
Excel.Application app;
private static CommandBarButton btn;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    app = Globals.ThisAddIn.Application;
    app.SheetSelectionChange += App_SheetSelectionChange;
}

private void App_SheetSelectionChange(object Sh, Excel.Range Target)
{            
     
          //右键重置避免按钮重复
  CommandBar currentMenuBar = app.CommandBars["cell"];
                currentMenuBar.Reset();
//删除右键
                foreach (CommandBarControl temp_contrl in currentMenuBar.Controls)
                {
                    string t = temp_contrl.Tag;
                    if (customMenuTags.Contains(t))
                    {
                        if (temp_contrl.Type == MsoControlType.msoControlPopup)
                        {
                            temp_contrl.Delete();
                            GC.Collect();
                            GC.WaitForPendingFinalizers();
                        }
                        else
                        {
                            try
                            {
                                temp_contrl.Delete();
                            }
                            catch { }
                        }
                    }
                }

  //解决事件连续触发
                        btn = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();


//定义右键菜单
                btn=
                            (CommandBarButton)currentMenuBar.Controls.Add(
                                MsoControlType.msoControlButton, missing, missing, missing);


                        btn.Tag = "test";
                        btn.Caption = "测试";
                        btn.Click += NewControl_Click;

//显示
foreach (CommandBarControl temp_contrl in currentMenuBar.Controls)
                {
                    string t = temp_contrl.Tag;
                    if (customMenuTags.Contains(t))
                    {
                        if (temp_contrl.Type == MsoControlType.msoControlButtonPopup)
                        {
                            ((CommandBarPopup)temp_contrl).Visible = true;
                        }
                        else
                        {
                            try
                            {
                                ((CommandBarButton)temp_contrl).Visible = true;
                            }
                            catch { }
                        }
                    }
                }
                
}

private void NewControl_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
    System.Windows.Forms.MessageBox.Show("Test");
}
 this.Shutdown += new EventHandler(ThisAddIn_Shutdown);
 
private void ThisAddIn_Shutdown(object sender, EventArgs e)
        {
           //卸载重置右键菜单
            CommandBar currentMenuBar = app.CommandBars["cell"];
            currentMenuBar.Reset();
        }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VSTO添加鼠标菜单,可以通过以下步骤实现: 1. 在 Visual Studio 中创建一个新的 VSTO 项目。 2. 在解决方案资源管理器中,项目,并选择“添加”->“新项”。 3. 在“添加新项”对话框中,选择“Office 项目”->“Outlook 项”,并选择“自定义 Outlook 邮件项”。 4. 在“自定义 Outlook 邮件项”对话框中,选择“邮件”并点“下一步”。 5. 在“选择项类型”页面中,选择“邮件窗体”,并点“下一步”。 6. 在“命名项”页面中,为新项命名并点“完成”。 7. 打开该项的代码视图,在 ThisAddIn.cs 文件中,添加以下代码: ``` private void ThisAddIn_Startup(object sender, System.EventArgs e) { Application.ItemContextMenuDisplay += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay); } void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection) { if (Selection.Count == 1 && Selection[1] is Outlook.MailItem) { CommandBarPopup popup = (CommandBarPopup)CommandBar.Controls.Add(MsoControlType.msoControlPopup, missing, missing, missing, true); popup.Caption = "My Custom Menu"; CommandBarButton button = (CommandBarButton)popup.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true); button.Caption = "My Custom Command"; button.Click += new _CommandBarButtonEvents_ClickEventHandler(button_Click); } } void button_Click(CommandBarButton Ctrl, ref bool CancelDefault) { MessageBox.Show("My Custom Command was clicked"); } ``` 上述代码中,我们通过 ThisAddIn_Startup 事件处理程序,订阅了 Application.ItemContextMenuDisplay 事件。在 Application_ItemContextMenuDisplay 事件处理程序中,我们检查当前选择是否为邮件,并创建一个自定义弹出菜单和一个自定义命令按钮。当用户单自定义命令按钮时,会触发 button_Click 事件处理程序,弹出一个消息框。 现在,您可以运行该项目,打开 Outlook 并尝试邮件,您将看到自定义的菜单和命令按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值