zoukankan      html  css  js  c++  java
  • VSTO为Excel快捷菜单添加项

    示例代码:

    public partial class Sheet4     {

            Office.CommandBar menuBar;

             Office.CommandBarPopup menuCustom;

            Office.CommandBarPopup menu1;

            Office.CommandBarButton menu2;

            Office.CommandBarButton menu1_1;

            Office.CommandBarButton menu1_2;

            Office.CommandBarButton menu1_3;

            Office.CommandBar bar1;

            Office.CommandBarPopup cmenu;

            Office.CommandBarButton cmenu_1;

            Office.CommandBarButton cmenu_2;

            private void Sheet4_Startup(object sender, System.EventArgs e)

            {

                /////////////////////////普通的菜单///////////////////////////////////

                //添加一级菜单

                menuBar = this.Application.CommandBars.ActiveMenuBar;

                menuCustom =

                    (Office.CommandBarPopup)menuBar.Controls.Add(

                    Office.MsoControlType.msoControlPopup,

                    missing, missing, missing, true);

                menuCustom.Caption = “自定义菜单”;

                //添加二级菜单

                menu1 =

                    (Office.CommandBarPopup)menuCustom.Controls.Add(

                    Office.MsoControlType.msoControlPopup,

                    missing, missing, missing, true);

                menu1.Caption = “菜单风格(&S)”;

                //添加同级的菜单

                menu2 =

                    (Office.CommandBarButton)menuCustom.Controls.Add(

                    Office.MsoControlType.msoControlButton,

                    missing, missing, missing, true);

                menu2.Caption = “自定义按钮类型的菜单(&S)”;

                //添加事件要设置Tag属性

                menu2.Tag = “custom”;

                //添加的事件处理函数

                menu2.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu2_Click);

                //添加三级菜单

                menu1_1 =

                    (Office.CommandBarButton)menu1.Controls.Add(

                    Office.MsoControlType.msoControlButton,

                    missing, missing, missing, true);

                menu1_1.Caption = “Up”;

                menu1_1.Tag = “MyUp”;

                menu1_1.State = Office.MsoButtonState.msoButtonUp;

                //添加事件

                menu1_1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu1_1_Click);

                menu1_2 =

                    (Office.CommandBarButton)menu1.Controls.Add(

                    Office.MsoControlType.msoControlButton,

                    missing, missing, missing, true);

                menu1_2.Caption = “Down”;

                menu1_2.Tag = “MyDown”;

                menu1_2.State = Office.MsoButtonState.msoButtonDown;

                //

                menu1_2.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu1_1_Click);

                menu1_3 =

                    (Office.CommandBarButton)menu1.Controls.Add(

                    Office.MsoControlType.msoControlButton,

                    missing, missing, missing, true);

                menu1_3.Caption = “Mixed”;

                menu1_3.State = Office.MsoButtonState.msoButtonMixed;

                //////////////////////////////上下文菜单/////////////////////////////////////////////////

                bar1 = Application.CommandBars["Cell"];

                if (bar1 == null)

                    return;

                //添加一级菜单

                           cmenu =

                    (Office.CommandBarPopup)bar1.Controls.Add(

                    Office.MsoControlType.msoControlPopup,

                    missing, missing, missing, true);

                cmenu.Caption = “自定义菜单”;

                //添加二级菜单

                cmenu_1 =

                    (Office.CommandBarButton)cmenu.Controls.Add(

                    Office.MsoControlType.msoControlButton,

                    missing, missing, missing, true);

                cmenu_1.Caption = “Up”;

                cmenu_1.State = Office.MsoButtonState.msoButtonUp;

                //添加二级菜单

                cmenu_2 =

                    (Office.CommandBarButton)cmenu.Controls.Add(

                    Office.MsoControlType.msoControlButton,

                    missing, missing, missing, true);

                cmenu_2.Caption = “Down”;

                cmenu_2.State = Office.MsoButtonState.msoButtonDown;

            }

            void menu1_1_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

            {

                //throw new NotImplementedException();

                if (Ctrl.Caption == “Up”)

                {                

          Ctrl.State = Office.MsoButtonState.msoButtonDown;

                    Ctrl.Caption = “Down”;

                }

                else

                {

                    Ctrl.State = Office.MsoButtonState.msoButtonUp;

                    Ctrl.Caption = “Up”;

                }

            }

            void menu2_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

            {

                //throw new NotImplementedException();

                MessageBox.Show(”这是一个自定义菜单项”);

            }

            private void Sheet4_Shutdown(object sender, System.EventArgs e)         {         }

            #region VSTO Designer generated code

            /// <summary>

            /// Required method for Designer support - do not modify

            /// the contents of this method with the code editor.

            /// </summary>

            private void InternalStartup()

            {

                this.Startup += new System.EventHandler(Sheet4_Startup);

                this.Shutdown += new System.EventHandler(Sheet4_Shutdown);

            }

            #endregion

  • 相关阅读:
    centos用yum安装mysql-server
    redis-dev
    quicktime player录屏没有声音的解决方法
    Mysql 5.7 系列命令 timestamp类型的字段不能设默认值为“0000-00-00 00:00:00” 要设为`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新',
    centos7+apache+svn配置 踩坑,注意权限问题。apache应用目录checkout应用 必须用这个命令:svn co file:///home/svn/test/ test ,通过svn add * &&commit 及任意修改都是不行的
    github webhook 实现代码自动部署 踩坑!! 附加git&coding webhook部署代码
    linux达人养成计划
    linux下软件安装知识整理
    PHP升级7.2之后需要注意的事情
    CentOS7 通过YUM安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/lisengl/p/2792153.html
Copyright © 2011-2022 走看看