private void CreateCommand(CommandBar cmdBar, Command command)
        {
            try
            {
                if (CommandDoesNotExist(command.CommandName))
                {
                    var            contextGUIDS = new object[] {};
                    EnvDTE.Command cmd          = _applicationObject.Commands.AddNamedCommand(_addInInstance, command.CommandName,
                                                                                              command.CommandText,
                                                                                              command.ToolTip,
                                                                                              true, 0, ref contextGUIDS,
                                                                                              (int)
                                                                                              vsCommandDisabledFlags.
                                                                                              vsCommandDisabledFlagsHidden);

                    if (cmdBar != null)
                    {
                        cmd.AddControl(cmdBar, 1);
                    }
                }
            }
            catch (Exception ex)
            {
                _messageBox.ShowError(ex.Message);
            }
        }
        /// <summary>
        /// Called when the AddIn is loaded. This method allows each of the commands to
        /// store member variables with the objects passed in and ensure that the menu
        /// items and commands have been properly added to the object model.
        /// </summary>
        /// <param name="application"> Root object in the application </param>
        /// <param name="connectMode"> 'Mode' in which the environment is starting up the addin </param>
        /// <param name="addIn"> Object representing this AddIn in the Object Model</param>
        public void OnConnection(EnvDTE._DTE application, Extensibility.ext_ConnectMode connectMode, EnvDTE.AddIn addIn)
        {
            try
            {
                m_applicationObject = (EnvDTE._DTE)application;
                m_addInInstance     = (EnvDTE.AddIn)addIn;

                m_strCommandName = "AMDeleteCourse";
                m_strName        = AMResources.GetLocalizedString("AMDeleteCourseName");
                m_strItemText    = AMResources.GetLocalizedString("AMDeleteCourseItemText");

                string            description          = AMResources.GetLocalizedString("AMDeleteCourseDescription");
                EnvDTE.Commands   commands             = null;
                EnvDTE.Command    command              = null;
                _CommandBars      officeCommandBars    = null;
                CommandBar        officeCommandBar     = null;
                CommandBarControl officeCommandControl = null;
                CommandBar        officeAcademic       = null;

                object [] contextGuids;
                contextGuids = new object[] { };

                commands = m_applicationObject.Commands;
                try
                {
                    command = commands.AddNamedCommand(m_addInInstance, m_strCommandName, m_strName, description,
                                                       false, 108, ref contextGuids, (int)(EnvDTE.vsCommandStatus.vsCommandStatusEnabled | EnvDTE.vsCommandStatus.vsCommandStatusSupported));

                    // Add the new command to the tools menu
                    officeCommandBars = m_applicationObject.CommandBars;
                    string amFacultyMenuItem = AMResources.GetLocalizedString("AMFacultyMenuItem");
                    try
                    {
                        officeAcademic = (CommandBar)officeCommandBars[amFacultyMenuItem];
                    }
                    catch
                    {
                    }
                    if (officeAcademic == null)
                    {
                        officeCommandBar = (CommandBar)officeCommandBars["Tools"];
                        officeAcademic   = (CommandBar)m_applicationObject.Commands.AddCommandBar(amFacultyMenuItem, EnvDTE.vsCommandBarType.vsCommandBarTypeMenu, officeCommandBar, 1);
                    }
                    officeCommandControl         = command.AddControl((object)officeAcademic, 1);
                    officeCommandControl.Caption = m_strItemText;
                }
                catch
                {
                    // Falling into this simply means that the command was already registered.
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception e = " + e.Message);
            }
        }
Beispiel #3
0
        public CommandBarControl AddButtonControl(
            DTE_Command InCmd)
        {
            EnvDTE.Command    cmd   = InCmd.command;
            CommandBarControl added =
                (CommandBarControl)cmd.AddControl(
                    mBar, Controls.Count + 1);

            added.Enabled = true;
            return(added);
        }
Beispiel #4
0
        /// <summary>
        /// Registers a command and places it on the Tools menu.
        /// </summary>
        public void OnConnection(EnvDTE._DTE application, Extensibility.ext_ConnectMode connectMode, EnvDTE.AddIn addIn)
        {
            m_applicationObject = (EnvDTE._DTE)application;
            m_addInInstance     = (EnvDTE.AddIn)addIn;

            m_strCommandName = "AMRemoveCourse";
            m_strName        = AMResources.GetLocalizedString("AMRemoveCourseName");
            m_strItemText    = AMResources.GetLocalizedString("AMRemoveCourseItemText");

            string strDescription = AMResources.GetLocalizedString("AMRemoveCourseDescription");

            EnvDTE.Commands commands = null;
            EnvDTE.Command  command  = null;
            Microsoft.Office.Core._CommandBars      officeCommandBars    = null;
            Microsoft.Office.Core.CommandBar        officeCommandBar     = null;
            Microsoft.Office.Core.CommandBarControl officeCommandControl = null;
            Microsoft.Office.Core.CommandBar        officeAcademic       = null;
            object [] contextGuids;
            contextGuids = new object[] { };

            commands = m_applicationObject.Commands;
            try
            {
                command = commands.AddNamedCommand(m_addInInstance, m_strCommandName, m_strName, strDescription,
                                                   false, 108, ref contextGuids, (int)(EnvDTE.vsCommandStatus.vsCommandStatusEnabled | EnvDTE.vsCommandStatus.vsCommandStatusSupported));

                // Add the new command to the tools menu
                officeCommandBars = m_applicationObject.CommandBars;
                string amStudentMenuItem = AMResources.GetLocalizedString("AMStudentMenuItem");
                try
                {
                    officeAcademic = (CommandBar)officeCommandBars[amStudentMenuItem];
                }
                catch
                {
                }
                if (officeAcademic == null)
                {
                    officeCommandBar = (CommandBar)officeCommandBars["Tools"];
                    officeAcademic   = (CommandBar)m_applicationObject.Commands.AddCommandBar(amStudentMenuItem, EnvDTE.vsCommandBarType.vsCommandBarTypeMenu, officeCommandBar, 1);
                }
                officeCommandControl         = command.AddControl((object)officeAcademic, 1);
                officeCommandControl.Caption = m_strItemText;
            }
            catch (System.Exception /*e*/)
            {
                // Falling into this simply means that the command was already registered.
            }
        }
Beispiel #5
0
        private void RunUISetup()
        {
            string toolsMenuName;

            try
            {
                //If you would like to move the command to a different menu, change the word "Tools" to the
                //  English version of the menu. This code will take the culture, append on the name of the menu
                //  then add the command to that menu. You can find a list of all the top-level menus in the file
                //  CommandBar.resx.
                string          resourceName;
                ResourceManager resourceManager = new ResourceManager(StudioshellResources, Assembly.GetExecutingAssembly());
                CultureInfo     cultureInfo     = new CultureInfo(_applicationObject.LocaleID);

                if (cultureInfo.TwoLetterISOLanguageName == "zh")
                {
                    System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
                    resourceName = String.Concat(parentCultureInfo.Name, "View");
                }
                else
                {
                    resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "View");
                }
                toolsMenuName = resourceManager.GetString(resourceName);
            }
            catch
            {
                //We tried to find a localized version of the word Tools, but one was not found.
                //  Default to the en-US word, which may work for the current culture.
                toolsMenuName = "View";
            }

            //Place the command on the tools menu.
            //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
            CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];

            //Find the Tools command bar on the MenuBar command bar:
            CommandBarControl viewControl = menuBarCommandBar.Controls[toolsMenuName];
            CommandBarPopup   viewPopup   = (CommandBarPopup)viewControl;

            //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //  just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                object[]  contextGUIDS = new object[] { };
                Commands2 commands     = (Commands2)_applicationObject.Commands;

                SafeDeleteCommand("CodeOwls.StudioShell.Connect.StudioShell");

                Command command = commands.AddNamedCommand2(_addInInstance,
                                                            "StudioShell", "StudioShell", "Opens Studio Shell",
                                                            false, 1,
                                                            ref contextGUIDS,
                                                            (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                command.Bindings = new object[] { "Global::Ctrl+Shift+Enter" };

                //Add a control for the command to the tools menu:
                if ((command != null) && (viewPopup != null))
                {
                    command.AddControl(viewPopup.CommandBar, 1);
                }

                SafeDeleteCommand("CodeOwls.StudioShell.Connect.Execute");
                command = commands.AddNamedCommand2(_addInInstance,
                                                    "Execute", "Exceute", "Executes PowerShell from a Macro",
                                                    false, 0,
                                                    ref contextGUIDS,
                                                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                                    (int)vsCommandStyle.vsCommandStyleText,
                                                    vsCommandControlType.vsCommandControlTypeButton);

                SafeDeleteCommand("CodeOwls.StudioShell.Connect.Cancel");
                command = commands.AddNamedCommand2(_addInInstance,
                                                    "Cancel", "Cancel Executing Pipeline", "Cancels the Currently Executing Studio Shell Pipeline",
                                                    false, 2,
                                                    ref contextGUIDS,
                                                    (int)vsCommandStatus.vsCommandStatusSupported,
                                                    (int)vsCommandStyle.vsCommandStylePictAndText,
                                                    vsCommandControlType.vsCommandControlTypeButton);
                command.Bindings = new object[] { "Global::Ctrl+Shift+C" };

                if ((command != null) && (viewPopup != null))
                {
                    command.AddControl(viewPopup.CommandBar, 2);
                }

                SafeDeleteCommand(StudioShellRestartCommandName);
                command = commands.AddNamedCommand2(_addInInstance,
                                                    "ResetRunspace", "Reset StudioShell", "Resets the StudioShell Runspace",
                                                    false, 3,
                                                    ref contextGUIDS,
                                                    (int)vsCommandStatus.vsCommandStatusSupported,
                                                    (int)vsCommandStyle.vsCommandStylePictAndText,
                                                    vsCommandControlType.vsCommandControlTypeButton);

                if ((command != null) && (viewPopup != null))
                {
                    command.AddControl(viewPopup.CommandBar, 2);
                }
            }
            catch (System.ArgumentException)
            {
                //If we are here, then the exception is probably because a command with that name
                //  already exists. If so there is no need to recreate the command and we can
                //  safely ignore the exception.
            }
        }