VSTO Excel开发(二):完美实现自定义Excel菜单

经过昨天不断看MSDN和网络资料,终于实现了上次提出的问题(完美实现自定义菜单),先给一个效果图:

VSTO Excel开发(二):完美实现自定义Excel菜单_第1张图片

仔细对比一下VSTO Excel开发(一)中的图,你会发行,这个菜单中多了分组线(也就是那横线)和子菜单(“报表”下还有菜单),这是怎么做到的呢,其实很简单只是将“BeginGroup”属性设为True即实现了分组;而子菜单的实现是将“报表”菜单类型设置为CommandBarPopup,继续上次的代码,需要继续增加的代码我还是用红色代表;


    public partial class ThisWorkbook
    {
        //**自定菜单
        //定义菜单变量
        //supplierCommand---供应商输入;materialCommand---物品表;questBuyBillCommand---申购单;quotationCommand-----报价单;purchaseOrderCommand--采购单;
        //

        private Office.CommandBarButton supplierCommand;
        private Office.CommandBarButton materialCommand;

        private Office.CommandBarButton questBuyBillCommand;
        private Office.CommandBarButton quotationCommand;
        private Office.CommandBarButton purchaseOrderCommand;

        private Office.CommandBarPopup reportPoup; //定义报表子菜单;
        private Office.CommandBarButton qbbReportCommand;//定义申购一览表;
        private Office.CommandBarButton qutationReportCommand; //定义报价一览表;

        //定义菜单Tag
        private string menuTag = "A unique tag";

        // 如果菜单存在则删除它.
        public void CheckIfMenuBarExists()
        {
            try
            {
                Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                    this.Application.CommandBars.ActiveMenuBar.FindControl(
                    Office.MsoControlType.msoControlPopup, System.Type.Missing, menuTag, true, true);

                if (foundMenu != null)
                {
                    foundMenu.Delete(true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // 如果菜单不存在则创建它.
        //supplierCommand---供应商输入;questBuyBillCommand---申购单;quotationCommand-----报价单;
        private void AddMenuBar()
        {
            try
            {
                Office.CommandBarPopup cmdBarControl = null;
                Office.CommandBar menubar = (Office.CommandBar)Application.CommandBars.ActiveMenuBar;
                int controlCount = menubar.Controls.Count;
                string menuCaption = "采购系统(&P)";
                // Add the menu.
                cmdBarControl = (Office.CommandBarPopup)menubar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, controlCount, true);
                cmdBarControl.Tag = menuTag;
                if (cmdBarControl != null)
                {
                    cmdBarControl.Caption = menuCaption;

                    // 添加“供应商资料”菜单命令.
                    supplierCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    supplierCommand.Caption = "供应商资料(&S)";
                    supplierCommand.Tag = "supplierCommand";
                    supplierCommand.FaceId = 0162;
                    supplierCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(supplierCommand_Click);

                    //添加“物品表”菜单命令
                    materialCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    materialCommand.Caption = "物品表(&M)";
                    materialCommand.Tag = "materialCommand";
                    materialCommand.FaceId = 0162;
                    materialCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(materialCommand_Click);

                    //添加“申购单”菜单命令
                    questBuyBillCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    questBuyBillCommand.Caption = "申购单输入(&Q)";
                    questBuyBillCommand.Tag = "questBuyBillCommand";
                    questBuyBillCommand.FaceId = 0162;
                    questBuyBillCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(questBuyBillCommand_Click);
                    questBuyBillCommand.BeginGroup = true; //将菜单分组;

                    //添加“报价单”菜单命令
                    quotationCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    quotationCommand.Caption = "报价单输入(&U)";
                    quotationCommand.Tag = "quotationCommand";
                    quotationCommand.FaceId = 0162;
                    quotationCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(quotationCommand_Click);

                    //添加"采购单"菜单命令
                    purchaseOrderCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    purchaseOrderCommand.Caption = "采购单输入(&P)";
                    purchaseOrderCommand.Tag = "purchaseOrderCommand";
                    purchaseOrderCommand.FaceId = 0162;
                    purchaseOrderCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(purchaseOrderCommand_Click);
                    //添加“报表”菜单
                    reportPoup = (Office.CommandBarPopup)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, true);
                    reportPoup.Caption = "报表";
                    reportPoup.Tag = "reportPoup";
                    reportPoup.BeginGroup = true;

                    //将“申购一览表”添加到“报表”菜单下一级
                    qbbReportCommand = (Office.CommandBarButton)reportPoup.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    qbbReportCommand.Caption = "申购一览表";
                    qbbReportCommand.Tag = "qbbReportCommand";
                    qbbReportCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(qbbReportCommand_Click);
                    //将“报价一览表”添加到“报表”菜单下一级
                    qutationReportCommand = (Office.CommandBarButton)reportPoup.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
                    qutationReportCommand.Caption = "报价一览表";
                    qutationReportCommand.Tag = "qutationReportCommand";
                    qutationReportCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(qutationReportCommand_Click);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        //点击菜单事件
        private void supplierCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            this.Close(true,missing,true );
        }

        void materialCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Globals.Sheet2.Activate();
        }

        void questBuyBillCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Globals.Sheet1.Range["A3", missing].Value2 = "The menu command was clicked.";
        }

        void quotationCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Globals.Sheet1.Range["A2", missing].Value2 = "The menu command was clicked.";
        }

        void purchaseOrderCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void qbbReportCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        void qutationReportCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            //当工作簿启动时初始化菜单
            CheckIfMenuBarExists();
            AddMenuBar();

        }

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

        }

                #region VSTO Designer generated code

        ///


        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisWorkbook_Startup);
            this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
        }

        #endregion

    }
}

OK,本着实用的原则,可以说已经实现了比较完美的菜单了;如果大家还想DIY的话,FaceId这个属性是可以自定义的,也就是你可以使用自己的图标,而不用系统内置的(如果大家要看比较全面的FaceId值所代表的图案,可以去kebabshopblues看看);

 引:http://itblog.spaces.live.com/

 

 

你可能感兴趣的:(excel,exception,报表,command,object,null)