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

原文地址:https://www.cnblogs.com/lisengl/p/2792153.html