private void AddToolbar()
        {
            string barName            = Properties.Resources.OrdersToolbar;
            string weeklyCaption      = Properties.Resources.WeeklyMenu;
            string unscheduledCaption = Properties.Resources.UnscheduledMenu;

            // Toolbars are unique for the whole application.
            // In case there are two workbooks running this code,
            // the second workbook should check first if the
            // toolbar is already there.
            // If it is not, it has to be created.
            // In any case, a handler must be added.
            Office.CommandBar commandBar = null;

            Office.CommandBarButton weeklyButton;
            Office.CommandBarButton unscheduledButton;

            for (int i = 1; i <= ThisApplication.CommandBars.Count; ++i)
            {
                if (ThisApplication.CommandBars[i].Name == barName)
                {
                    commandBar = ThisApplication.CommandBars[i];
                    break;
                }
            }

            if (commandBar == null)
            {
                commandBar = this.Application.CommandBars.Add(barName, Office.MsoBarPosition.msoBarTop, missing, true);
            }


            if (commandBar.FindControl(missing, missing, weeklyCaption, missing, false) == null)
            {
                weeklyButton = (Office.CommandBarButton)commandBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, missing);

                weeklyButton.Caption = weeklyCaption;

                weeklyButton.Picture = ExcelHelpers.Convert(toolBarImages.Images["CreateWeeklyOrder"]);
                weeklyButton.Mask    = ExcelHelpers.Convert(toolBarImages.Images["CreateWeeklyOrderMask"]);
                weeklyButton.Tag     = weeklyCaption;
            }
            else
            {
                weeklyButton = (Office.CommandBarButton)commandBar.Controls[weeklyCaption];
            }

            if (commandBar.FindControl(missing, missing, unscheduledCaption, missing, false) == null)
            {
                unscheduledButton = (Office.CommandBarButton)commandBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, missing);

                unscheduledButton.Caption = unscheduledCaption;

                unscheduledButton.Picture = ExcelHelpers.Convert(this.toolBarImages.Images["CreateUnscheduledOrder"]);
                unscheduledButton.Mask    = ExcelHelpers.Convert(this.toolBarImages.Images["CreateUnscheduledOrderMask"]);
                unscheduledButton.Tag     = unscheduledCaption;
            }
            else
            {
                unscheduledButton = (Office.CommandBarButton)commandBar.Controls[unscheduledCaption];
            }

            commandBar.Visible = true;

            this.weeklyToolbarButton      = weeklyButton;
            this.unscheduledToolbarButton = unscheduledButton;

            this.toolBar = commandBar;
        }
Ejemplo n.º 2
0
        private void AddToolbar()
        {
            string barName            = Properties.Resources.OrdersToolbar;
            string weeklyCaption      = Properties.Resources.WeeklyMenu;
            string unscheduledCaption = Properties.Resources.UnscheduledMenu;

            // 工具栏对于整个应用程序是唯一的。
            // 如果有两个工作簿运行此代码,
            // 第二个工作簿应先检查工具栏
            // 是否已存在。
            // 如果不存在,则必须创建。
            // 在任何情况下,都必须添加一个处理程序。
            Office.CommandBar commandBar = null;

            Office.CommandBarButton weeklyButton;
            Office.CommandBarButton unscheduledButton;

            for (int i = 1; i <= ThisApplication.CommandBars.Count; ++i)
            {
                if (ThisApplication.CommandBars[i].Name == barName)
                {
                    commandBar = ThisApplication.CommandBars[i];
                    break;
                }
            }

            if (commandBar == null)
            {
                commandBar = this.Application.CommandBars.Add(barName, Office.MsoBarPosition.msoBarTop, missing, true);
            }


            if (commandBar.FindControl(missing, missing, weeklyCaption, missing, false) == null)
            {
                weeklyButton = (Office.CommandBarButton)commandBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, missing);

                weeklyButton.Caption = weeklyCaption;

                weeklyButton.Picture = ExcelHelpers.Convert(toolBarImages.Images["CreateWeeklyOrder"]);
                weeklyButton.Mask    = ExcelHelpers.Convert(toolBarImages.Images["CreateWeeklyOrderMask"]);
                weeklyButton.Tag     = weeklyCaption;
            }
            else
            {
                weeklyButton = (Office.CommandBarButton)commandBar.Controls[weeklyCaption];
            }

            if (commandBar.FindControl(missing, missing, unscheduledCaption, missing, false) == null)
            {
                unscheduledButton = (Office.CommandBarButton)commandBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, missing);

                unscheduledButton.Caption = unscheduledCaption;

                unscheduledButton.Picture = ExcelHelpers.Convert(this.toolBarImages.Images["CreateUnscheduledOrder"]);
                unscheduledButton.Mask    = ExcelHelpers.Convert(this.toolBarImages.Images["CreateUnscheduledOrderMask"]);
                unscheduledButton.Tag     = unscheduledCaption;
            }
            else
            {
                unscheduledButton = (Office.CommandBarButton)commandBar.Controls[unscheduledCaption];
            }

            commandBar.Visible = true;

            this.weeklyToolbarButton      = weeklyButton;
            this.unscheduledToolbarButton = unscheduledButton;

            this.toolBar = commandBar;
        }