VSTO Excel開發(二):完美實現自定義Excel菜單

經過昨天不斷看MSDN和網絡資料,終於實現了上次提出的問題(完美實現自定義菜單),先給一個效果圖:

item5

仔細對比一下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

        /// <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(ThisWorkbook_Startup);
            this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
        }

        #endregion

    }
}

OK,本着實用的原則,可以說已經實現了比較完美的菜單了;如果大家還想DIY的話,FaceId這個屬性是可以自定義的,也就是你可以使用自己的圖標,而不用系統內置的(如果大家要看比較全面的FaceId值所代表的圖案,可以去kebabshopblues看看);

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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章