본문 바로가기

Etc/MicroSoft Office

MenuBar에 추가하기


// If the menu already exists, remove it.

        private void CheckIfMenuBarExists()

        {

            try

            {

                //Menu가 있는지 체크 해서 있으면 지워버렷!!

                CommandBarPopup foundMenu = (CommandBarPopup)this.Application.CommandBars.ActiveMenuBar.FindControl(MsoControlType.msoControlPopup, System.Type.Missing, menuTag, true, true);

                if (foundMenu != null)

                {

                    foundMenu.Delete(true);

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

 

        // Create the menu, if it does not exist.

        private void AddMenuBar()

        {

            try

            {

                CommandBarPopup cmdBarControl = null;

                CommandBar menubar = (CommandBar)Application.CommandBars.ActiveMenuBar;

               

                int controlCount = menubar.Controls.Count;

                string menuCaption = "&New Menu";

 

                // Add the menu.

                cmdBarControl = (CommandBarPopup)menubar.Controls.Add(MsoControlType.msoControlPopup, missing, missing, controlCount, true);

 

                if (cmdBarControl != null)

                {

                    cmdBarControl.Caption = menuCaption;

 

                    // Add the menu command.

                    CommandBarButton menuCommand = AddMenuCommandButton(cmdBarControl , "&New Menu Command" , "NewMenuCommand" , 65);

                    CommandBarButton myCommand = AddMenuCommandButton(cmdBarControl , "H&an Yun Jin" ,"HanYunJin",66);

                   

                    menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);

                    myCommand.Click += new _CommandBarButtonEvents_ClickEventHandler(myCommand_Click);

 

                }

            }

            catch (Exception e)

            {

                MessageBox.Show(e.Message);

            }

        }

 

      

 

        private CommandBarButton AddMenuCommandButton(CommandBarPopup cmdBarControl, string caption, string tag, int faceID)

        {

            CommandBarButton tempButton;

            tempButton = (CommandBarButton)cmdBarControl.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true);

            tempButton.Caption = caption;

            tempButton.Tag = tag;

            tempButton.FaceId = faceID;

           

            return tempButton;

        }

워워~ 이제야 메뉴에 추가 했넹 계속 고고~