/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>Root object of the host application.</param>
        /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance     = (AddIn)addInInst;

            if (connectMode == ext_ConnectMode.ext_cm_Startup || connectMode == ext_ConnectMode.ext_cm_AfterStartup)
            {
                object[]  contextGUIDS = new object[] { };
                Commands2 commands     = (Commands2)_applicationObject.Commands;

                // Add new toolbar and set enabled and visible
                CommandBars commandBars = _applicationObject.CommandBars as CommandBars;
                CommandBar  toolbar     = commandBars.Add("AlphaToolbar", MsoBarPosition.msoBarTop, System.Type.Missing, true);
                toolbar.Visible = true;
                toolbar.Enabled = true;

                // Make sure the alpha command doesn't already exist
                Command commandAlpha = null, commandOmega = null;

                try
                {
                    commandAlpha = commands.Item("AlphaBlendToolbar.Connect.AlphaButton");
                }
                catch (System.ArgumentException)
                {
                    // Load the alpha button image from a 32-bit alpha-transparent portable network graphic file which is an
                    // embedded resource in this project.  This image could also be loaded directly from disk.
                    System.Drawing.Bitmap alpha = new System.Drawing.Bitmap(typeof(AlphaBlendToolbar.Connect), @"alpha.png");

                    // Create the alpha button command, specifying the command name, button text, tooltip, bitmap image,
                    // status, style, and control type.  Add this command to the AlphaToolbar.
                    commandAlpha = commands.AddNamedCommand2(_addInInstance, "AlphaButton", "Alpha", "Alpha Command", false, alpha, ref contextGUIDS,
                                                             (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                }
                commandAlpha.AddControl(toolbar, 1);

                // Make sure the omega command doesn't already exist
                try
                {
                    commandOmega = commands.Item("AlphaBlendToolbar.Connect.OmegaButton");
                }
                catch (System.ArgumentException)
                {
                    // Load the omega button image from a 32-bit alpha-transparent portable network graphic file which is an
                    // embedded resource in this project.  This image could also be loaded directly from disk.
                    System.Drawing.Bitmap omega = new System.Drawing.Bitmap(typeof(AlphaBlendToolbar.Connect), @"omega.png");

                    // Create the omega button command, specifying the command name, button text, tooltip, bitmap image,
                    // status, style, and control type.  Add this command to the AlphaToolbar.
                    commandOmega = commands.AddNamedCommand2(_addInInstance, "OmegaButton", "Omega", "Omega Command", false, omega, ref contextGUIDS,
                                                             (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                }
                commandOmega.AddControl(toolbar, 2);
            }
        }
示例#2
0
        private bool CommandIsInstalled(Action action)
        {
            bool found = true;

            try
            {
                var findCommand = _commands.Item(action.FullName, 1);
            }
            catch (Exception ex)
            {
                found = false;
                Error(action.DisplayName + " not found: ", ex);
            }
            return(found);
        }
示例#3
0
        private string GetCommandName(string aGuid, int aId)
        {
            try
            {
                if (null == aGuid)
                {
                    return(string.Empty);
                }

                if (null == mCommand)
                {
                    return(string.Empty);
                }

                if (CommandIds.ids.Contains(aId) == false)
                {
                    return(string.Empty);
                }

                Command cmd = mCommand.Item(aGuid, aId);
                if (null == cmd)
                {
                    return(string.Empty);
                }

                return(cmd.Name);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(string.Empty);
        }
示例#4
0
        bool ExecuteCommand(string command)
        {
            try
            {
                // Get an instance of the currently running Visual Studio IDE.
                DTE2 dte2;
                dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;

                // 2 checks to make sure the command exists in the IDE.
                Commands2 commands = (Commands2)(dte2.Commands);
                if (commands == null)
                {
                    return(false);
                }

                Command dte_command = commands.Item(command, 0);
                if (dte_command == null)
                {
                    return(false);
                }

                dte2.ExecuteCommand(command);
                return(dte_command.IsAvailable);
            }
            catch (Exception ex)
            {
                if (ex.HResult != -2147467259)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                return(false);
            }
        }
示例#5
0
        /// <summary>
        /// 从菜单或工具条中删除指定的命令
        /// </summary>
        /// <param name="CmdName"></param>
        /// <param name="SubItemName"></param>
        /// <param name="Name"></param>
        /// <param name="Caption"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Exception DeleteCommand(string CmdName, string SubItemName, string Name, string Caption)
        {
            //CmdName 工具条名称, SubItemName 工具条子项目名称,Name 要添加的项目名称,
            //Caption 要添加的项目标题.此方法内用于删除该按钮/项
            Exception   e            = null;
            Commands2   Cmds         = (Commands2)chDTE.Commands;
            CommandBars CmdBars      = (CommandBars)chDTE.CommandBars;
            CommandBar  mnuBarCmdBar = CmdBars[CmdName];
            //菜单
            CommandBarControl CmdCtrl  = mnuBarCmdBar.Controls[SubItemName];
            CommandBarPopup   CmdPopup = (CommandBarPopup)CmdCtrl;

            try
            {
                Cmds.Item("KeelKit.Commands." + Name, 0).Delete();
            }
            catch (Exception ex)
            {
                e = ex;
            }
            try
            {
                CommandBarControl chCmdConfig = CmdPopup.Controls[Caption];
                chCmdConfig.Delete(null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            return(e);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="VSMenuCommand"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="displayName">The display name.</param>
        /// <param name="description">The description.</param>
        public VSMenuCommand(VSMenu menu, string name, string displayName, string description)
        {
            _menu        = menu;
            _name        = name;
            _displayName = displayName;
            _description = description;

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

            Commands2 commands = (Commands2)menu.VSAddin.ApplicationObject.Commands;

            try
            {
                _command = commands.AddNamedCommand2(menu.VSAddin.AddInInstance,
                                                     name, displayName, description,
                                                     true, 0, ref contextGUIDS,
                                                     (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                                     (int)vsCommandStyle.vsCommandStylePictAndText,
                                                     vsCommandControlType.vsCommandControlTypeButton);
            }
            catch
            {
                // If we get here then the command name probably already exists.
                _command = commands.Item(menu.VSAddin.AddInInstance.ProgID + "." + name, 0);
            }

            _button = (CommandBarButton)_command.AddControl(_menu.Popup.CommandBar, 1);
        }
示例#7
0
        private bool CreateCommand()
        {
            try
            {
                Commands2 hCommands = m_hApplicationObject.Commands as Commands2;
                m_hCommand = hCommands.Item("CodeBeautifier.Connect." + m_strCommandName, 0);

                return(false);
            }
            catch (ArgumentException)
            {
                // Build command
                object[]  contextGUIDS = new object[] { };
                Commands2 hCommands    = m_hApplicationObject.Commands as Commands2;
                m_hCommand = hCommands.AddNamedCommand2(m_hAddInInstance,
                                                        (String)m_strCommandName,
                                                        (String)m_strCommandCaption,
                                                        (String)m_strCommandName,
                                                        true,
                                                        m_iIconIdx,
                                                        contextGUIDS,
                                                        (int)vsCommandStatus.vsCommandStatusSupported,
                                                        (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                return(true);
            }
        }
示例#8
0
        protected string GetCommandName(string aGuid, int aId)
        {
            try
            {
                if (null == aGuid)
                {
                    return(string.Empty);
                }

                if (null == mCommand)
                {
                    return(string.Empty);
                }

                Command cmd = mCommand.Item(aGuid, aId);
                if (null == cmd)
                {
                    return(string.Empty);
                }

                return(cmd.Name);
            }
            catch (Exception) { }

            return(string.Empty);
        }
示例#9
0
 private void RemoveCommandIfAlreadyExits(string command_name)
 {
     try
     {
         Command cmd = _SSMS_commands_collection.Item(_ssms.addin.ProgID + "." + command_name);
         cmd.Delete();
     }
     catch { }
 }
示例#10
0
        /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
        /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
        /// <param term='custom'>Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
        {
            Commands2 commands = (Commands2)_applicationObject.Commands;

            try
            {
                Command addinCommand = commands.Item("SSMSAddin.Connect.ViewReferencedValue");
                addinCommand.Delete();
            }
            catch (System.ArgumentException e)
            {
                System.Diagnostics.Debug.Print("Error deleting commands on disconnection!");
            }
        }
示例#11
0
        public void RegisterCommand()
        {
            Commands2 commands = (Commands2)m_application.Commands;

            object[] context_GUIDS = new object[0];
            try
            {
                m_command = commands.Item(m_fullName, 0);
            }
            catch
            {
                m_command = commands.AddNamedCommand2(m_addIn, m_name, m_buttonText, m_toolTip, true, m_bitmapNumber, ref context_GUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
            }
        }
示例#12
0
        private void InstallActions()
        {
            if (commandPresentations != null)
            {
                return;
            }

            commandPresentations = new Dictionary <string, CommandPresentation>();

            foreach (var commandHandle in commandHandles)
            {
                CommandTraits traits = commandHandle.GetTraits();

                Commands2 commands = (Commands2)shell.DTE.Commands;

                Command command;
                try
                {
                    command = commands.Item(traits.CommandName, 0);
                }
                catch
                {
                    object[] contextGuids = null;
                    command = commands.AddNamedCommand2(shell.ShellAddIn,
                                                        traits.CommandName,
                                                        traits.Caption,
                                                        traits.Tooltip,
                                                        true, 59, ref contextGuids,
                                                        (int)ToVsCommandStatus(traits.Status),
                                                        (int)ToVsCommandStyle(traits.Style),
                                                        ToVsCommandControlType(traits.ControlType));
                }

                CommandBarButton[] controls = GenericCollectionUtils.ConvertAllToArray(traits.CommandBarPaths,
                                                                                       commandBarPath =>
                {
                    CommandBar commandBar             = GetCommandBar(commandBarPath);
                    CommandBarButton commandBarButton = GetOrCreateCommandBarButton(commandBar, command, commands);
                    return(commandBarButton);
                });

                CommandPresentation presentation = new CommandPresentation(commandHandle, command, controls);
                presentation.Icon    = traits.Icon;
                presentation.Caption = traits.Caption;
                presentation.Tooltip = traits.Tooltip;
                presentation.Status  = traits.Status;

                commandPresentations.Add(traits.CommandName, presentation);
            }
        }
示例#13
0
        /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
        /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
        /// <param term='custom'>Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
        {
            Commands2 commands = (Commands2)_applicationObject.Commands;

            try
            {
                Command addinCommand = commands.Item("MyAddin1.Connect.MyAddin1");
                addinCommand.Delete();

                Command addinCommand2 = commands.Item("MyAddin1.Connect.MyAddin1ToolWindowForm");
                addinCommand2.Delete();
            }

            catch (System.ArgumentException e)
            {
                System.Diagnostics.Debug.Print("Error deleting commands ond isconnection!");
            }

            //Delete our custom toolbar
            if (ourCustomToolbar != null)
            {
                ourCustomToolbar.Delete();
            }
        }
示例#14
0
        /// <summary>
        /// Gets the Command specified by shortName..
        /// </summary>
        /// <param name="shortName">Command short name</param>
        /// <returns>Returns the Command on success or null when the Command does not exist.</returns>
        public static Command GetCommand(this Commands2 me, string fullname)
        {
            Command command = null;

            // Try to retrieve the command, ignore any exception that would
            // occur if the command was not created yet.
            try
            {
                command = me.Item(fullname, -1);
            }
            catch
            { }

            return(command);
        }
示例#15
0
        private Command GetCommand(string commandName)
        {
            Commands2 commands = (Commands2)m_application.Commands;

            string fullName = m_connectPath + "." + commandName;

            try
            {
                Command command = commands.Item(fullName, 0);
                return(command);
            }
            catch (System.ArgumentException)
            {
                return(null);
            }
        }
示例#16
0
        private Command AddCommand(CommandBar toolbar, string key, string name, string tooltip,
                                   vsCommandControlType ctype, int toolbarpos, vsCommandStyle cmdstyle, int faceID, out object controlCreated)
        {
            object[]  contextGUIDS = new object[] { };
            Commands2 commands     = (Commands2)_applicationObject.Commands;

            // Add a command to the Commands collection:
            Command command = null;

            try
            {
                command = commands.Item(String.Format("{0}.{1}", _addInInstance.ProgID, key), -1);
            }
            catch (ArgumentException)
            {
            }

            if (command == null)
            {
                command = commands.AddNamedCommand2(_addInInstance, key, name, tooltip, false, faceID, ref contextGUIDS,
                                                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                                    (int)cmdstyle, ctype);
            }

            if (command != null && toolbar != null && toolbarpos > 0)
            {
                controlCreated = command.AddControl(toolbar, toolbarpos);
                CommandBarControl aux = controlCreated as CommandBarControl;
                if (aux != null)
                {
                    _controlsCreated.Add(aux);
                }

                //if (controlCreated is CommandBarButton)
                //{
                //  PictureManager.CommandButtons.Add(key, controlCreated as CommandBarButton);
                //  PictureManager.SetPicture(key, true);
                //}
            }
            else
            {
                controlCreated = null;
            }

            return(command);
        }
        public VSMenuCommand(VSMenu menu, string name, string displayName, string description)
        {
            this._menu        = menu;
            this._name        = name;
            this._displayName = displayName;
            this._description = description;
            object[]  array    = new object[0];
            Commands2 commands = (Commands2)menu.VSAddin.ApplicationObject.Commands;

            try
            {
                this._command = commands.AddNamedCommand2(menu.VSAddin.AddInInstance, name, displayName, description, true, 0, ref array, 3, 3, vsCommandControlType.vsCommandControlTypeButton);
            }
            catch
            {
                this._command = commands.Item(menu.VSAddin.AddInInstance.ProgID + "." + name, 0);
            }
            this._button = (CommandBarButton)this._command.AddControl(this._menu.Popup.CommandBar, 1);
        }
示例#18
0
        /// <summary>
        /// Adds a command bar button.
        /// </summary>
        /// <param name="commands">The commands object.</param>
        /// <param name="commandBar">The command bar.</param>
        /// <param name="commandName">Name of the command.</param>
        /// <param name="description">The description.</param>
        /// <param name="picture">The picture.</param>
        /// <param name="mask">The mask.</param>
        /// <returns></returns>
        private CommandBarButton AddCommandBarButton(Commands2 commands, CommandBar commandBar, string commandName, string description, Bitmap picture, Bitmap mask)
        {
            Command command = null;

            object[] contextGUIDS = null;

            try
            {
                command = commands.Item(addInInstance.ProgID + "." + commandName, 0);
            }
            catch
            {
            }

            if (command == null)
            {
                vsCommandStyle commandStyle = vsCommandStyle.vsCommandStylePict;

                command = commands.AddNamedCommand2(addInInstance,
                                                    commandName,
                                                    string.Empty,
                                                    description,
                                                    true,
                                                    null,
                                                    ref contextGUIDS,
                                                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                                    (int)commandStyle,
                                                    vsCommandControlType.vsCommandControlTypeButton);
            }

            CommandBarButton control = (CommandBarButton)command.AddControl(commandBar, commandBar.Controls.Count + 1);

            control.BeginGroup = true;

            if (picture != null)
            {
                control.Picture = (StdPicture)ImageConverter.GetIPictureDispFromImage(picture);
                control.Mask    = (StdPicture)ImageConverter.GetIPictureDispFromImage(mask);
            }

            return(control);
        }
示例#19
0
        /// <summary>
        /// Adds the command bar popup (menu item).
        /// </summary>
        /// <param name="commands">The commands object.</param>
        /// <param name="commandBar">The command bar.</param>
        /// <param name="name">The name.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="description">The description.</param>
        /// <param name="picture">The picture.</param>
        /// <param name="mask">The mask.</param>
        private void AddCommandBarPopup(Commands2 commands, CommandBarPopup commandBar, string name, string caption, string description, Bitmap picture, Bitmap mask)
        {
            Command command = null;

            object[] contextGUIDS = null;

            try
            {
                command = commands.Item(addInInstance.ProgID + "." + name, 0);
            }
            catch
            {
            }

            if (command == null)
            {
                vsCommandStyle commandStyle = (picture == null) ? vsCommandStyle.vsCommandStyleText : vsCommandStyle.vsCommandStylePictAndText;

                command = commands.AddNamedCommand2(addInInstance,
                                                    name,
                                                    caption,
                                                    description,
                                                    true,
                                                    null,
                                                    ref contextGUIDS,
                                                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                                    (int)commandStyle,
                                                    vsCommandControlType.vsCommandControlTypeButton);
            }

            CommandBarButton control = (CommandBarButton)command.AddControl(commandBar.CommandBar, 1);

            control.Caption = caption;

            if (picture != null)
            {
                control.Picture = (StdPicture)ImageConverter.GetIPictureDispFromImage(picture);
                control.Mask    = (StdPicture)ImageConverter.GetIPictureDispFromImage(mask);
            }
        }
        public static bool GetCommandByID(Commands2 command2, string aGuid, int aId, out Command command)
        {
            command = null;

            if (string.IsNullOrWhiteSpace(aGuid))
            {
                return(false);
            }

            if (null == command2)
            {
                return(false);
            }

            command = command2.Item(aGuid, aId);

            if (null == command)
            {
                return(false);
            }

            return(true);
        }
示例#21
0
        /// <summary>
        /// 删除指定菜单或工具条中的命令.
        /// </summary>
        /// <param name="Owner">所有者</param>
        /// <param name="Name">名称.</param>
        /// <param name="Caption">标题</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static Exception DeleteCommand(CommandBarControl Owner, string Name, string Caption)
        {
            Exception e = null;

            try
            {
                //CmdName 工具条名称, SubItemName 工具条子项目名称,Name 要添加的项目名称,
                //Caption 要添加的项目标题.此方法内用于删除该按钮/项

                Commands2   Cmds         = (Commands2)chDTE.Commands;
                CommandBars CmdBars      = (CommandBars)chDTE.CommandBars;
                CommandBar  mnuBarCmdBar = CmdBars["MenuBar"];
                //菜单
                CommandBarControl CmdCtrl  = Owner;
                CommandBarPopup   CmdPopup = (CommandBarPopup)CmdCtrl;

                Cmds.Item("KeelKit.Connect." + Name, 0).Delete();
            }
            catch (Exception ex)
            {
                e = ex;
            }
            return(e);
        }
示例#22
0
        /// <summary>
        /// Tries to find current command in specified command collection.
        /// </summary>
        private Command FindCommand(Commands2 commands)
        {
            for (int i = commands.Count; i >= 1; i--)
            {
                Command command = commands.Item(i);
                if (command.Name == FullCommandName)
                {
                    return command;
                }
            }

            return null;
        }
示例#23
0
 private void _DeleteIndividualCommand(Commands2 cmds, String fullCommandName)
 {
     try {
         Command cmdToDelete = cmds.Item(fullCommandName, -1);
         cmdToDelete.Delete();
     } catch (ArgumentException) {
         // The ArgumentException will be thrown if the command does not
         // exist. It is fine to eat this exception as that means it's
         // the first run of this add in.
     }
 }
示例#24
0
        }     // AddCommandBar

        /// <summary>Add one of the commands, dealing with the errors that might result.</summary>
        /// <remarks>First version from MSDN Magazin 02/02</remarks>
        /// <param name="applicationObject">The host application.</param>
        /// <param name="addInInstance"></param>
        /// <param name="szName">name of the command being added</param>
        /// <param name="IconNr">Number of the corresponding icon in the resource dll.</param>
        /// <param name="szButtonText">text displayed on menu or button</param>
        /// <param name="szToolTip">text displayed in tool tip</param>
        /// <param name="szKey">default key assignment, or empty string if none</param>
        /// <param name="CmdBars">optional CommandBars where this command should be added.</param>
        public static Command AddCommand(DTE2 applicationObject, AddIn addInInstance,
                                         String szName, int IconNr, String szButtonText, String szToolTip, String szKey,
                                         params CommandBar[] CmdBars)
        {
            Command cmd = null;

            object[]  contextGUIDS = new object[] { };
            Commands2 commands     = (Commands2)applicationObject.Commands;

            // The IDE identifies commands by their full name, which include the add-in name
            try
            {
                cmd = commands.Item("Zippy.Chirp." + szName, -1);
                cmd.Delete();
            }
            catch (ArgumentException)
            {
                // Thrown if command doesn't already exist.
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message, "ChirpyPopupAddIn.AddCommand");
            }     // try

            // The IDE in Visual Studio8 identifies commands by their full name, which include the add-in name
            try
            {
                cmd = commands.Item("Zippy.Chirp.Chirp." + szName, -1);
                cmd.Delete();
            }
            catch (ArgumentException)
            {
                // Thrown if command doesn't already exist.
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message, "ChirpyPopupAddIn.AddCommand");
            }     // try

            try
            {
                // Icons from the satelite dll.
                cmd = commands.AddNamedCommand2(addInInstance, szName, szButtonText, szToolTip,
                                                false, IconNr, ref contextGUIDS,
                                                (int)vsCommandStatus.vsCommandStatusSupported +
                                                (int)vsCommandStatus.vsCommandStatusEnabled,
                                                (int)vsCommandStyle.vsCommandStylePictAndText,
                                                vsCommandControlType.vsCommandControlTypeButton);
            }
            catch (ArgumentException exArg)
            {
                // Thrown if command already exist.
                Debug.WriteLine(exArg.Message, "ChirpyPopupAddIn.AddCommand");
                //System.Windows.Forms.MessageBox.Show("AddCommand=" + exArg.ToString());
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message, "ChirpyPopupAddIn.AddCommand");
                //System.Windows.Forms.MessageBox.Show("AddCommand=" + ex.ToString());
            }     // try

            if ((szKey != null) && (szKey != ""))
            {
                // a default keybinding specified
                object[] bindings;
                bindings = (object[])cmd.Bindings;
                if (0 >= bindings.Length)
                {
                    // there is no preexisting key binding, so add the default
                    bindings     = new object[1];
                    bindings[0]  = (object)szKey;
                    cmd.Bindings = (object)bindings;
                } // if
            }     // if

            // Append Control at end of CommandBar
            foreach (CommandBar c in CmdBars)
            {
                if (c != null)
                {
                    cmd.AddControl(c, c.Controls.Count + 1);
                }
            } // foreach
            return(cmd);
        }     // AddCommand
示例#25
0
        /// <summary>Registers refactoring actions with Visual Studio</summary>
        private void SetupRefactorActions()
        {
            object[]  contextGUIDS    = new object[] { };
            Commands2 commands        = (Commands2)applicationObject.Commands;
            Command   refactorCommand = null;

            IList <CommandBar> targets = new List <CommandBar>();

            try {
                try {
                    CommandBar menuBarCommandBar = ((CommandBars)applicationObject.CommandBars)["MenuBar"];
                    targets.Add(((CommandBarPopup)menuBarCommandBar.Controls[FindLocalizedMenuName("Refactor")]).CommandBar);
                } catch { }
                try {
                    targets.Add(((CommandBars)applicationObject.CommandBars)["Refactor"]);
                } catch { }
                try {
                    targets.Add(((CommandBars)applicationObject.CommandBars)["XAML Editor"]);
                } catch { }
                try {
                    targets.Add(((CommandBars)applicationObject.CommandBars)["ASPX Context"]);
                } catch { }
                try {
                    targets.Add(((CommandBars)applicationObject.CommandBars)["ASPX Code Context"]);
                } catch { }
                try {
                    targets.Add(((CommandBars)applicationObject.CommandBars)["ASPX VB Code Context"]);
                } catch { }
                try {
                    targets.Add(((CommandBars)applicationObject.CommandBars)["HTML Context"]);
                } catch { }

                foreach (var target in targets)
                {
                    while (true)
                    {
                        try {
                            CommandBarControl ctl = (CommandBarControl)(target.Controls[Strings.RefactorCommandText]);
                            ctl.Delete(false);
                        } catch (ArgumentException) {
                            break;
                        }
                    }
                }

                // Create refactor command if necessary
                try {
                    refactorCommand = commands.AddNamedCommand2(addInInstance,
                                                                Strings.RefactorCommandName,
                                                                Strings.RefactorCommandText,
                                                                Strings.RefactorCommandToolTip,
                                                                true, 69, ref contextGUIDS, (int)(vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported), (int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
                    if (!String.IsNullOrEmpty(Strings.RefactorCommandHotkey))
                    {
                        refactorCommand.Bindings = new object[] { Strings.RefactorCommandHotkey };
                    }
                } catch (ArgumentException) {
                    refactorCommand = commands.Item(addInInstance.ProgID + "." + Strings.RefactorCommandName, -1);
                }

                foreach (var target in targets)
                {
                    refactorCommand.AddControl(target, target.Controls.Count + 1);
                }
            } catch (Exception e) {
                System.Diagnostics.Trace.TraceError(e.ToString());
                throw;
            }
        }
示例#26
0
        /// <summary>IDTExtensibility2 인터페이스의 OnConnection 메서드를 구현합니다. 추가 기능이 로드되고 있다는 알림 메시지를 받습니다.</summary>
        /// <param term='application'>호스트 응용 프로그램의 루트 개체입니다.</param>
        /// <param term='connectMode'>추가 기능이 로드되는 방법을 설명합니다.</param>
        /// <param term='addInInst'>이 추가 기능을 나타내는 개체입니다.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            AddNewSubCommand("OpenSolutionFolderExplorer", "Open Solution Folder with Explorer", 65);
            AddNewSubCommand("OpenSolutionFolderCmd", "Open Command Prompt on Solution Folder", 65);
            AddNewSubCommand("OpenCorrespondingFile", "Toggle Source/Header", 65);
            AddNewSubCommand("OpenFileInSolution", "Quick Open File In Solution", 65);
            AddNewSubCommand("About", "About ZAssist...", 65);
            //AddNewSubCommand("QuickFindFunction", "Quick Find Function", 65);

            _applicationObject = (DTE2)application;
            _addInInstance     = (AddIn)addInInst;
            if (connectMode == ext_ConnectMode.ext_cm_UISetup ||
                connectMode == ext_ConnectMode.ext_cm_Startup ||
                connectMode == ext_ConnectMode.ext_cm_AfterStartup)
            {
                object[]  contextGUIDS = new object[] { };
                Commands2 commands     = (Commands2)_applicationObject.Commands;
                string    toolsMenuName;

                try
                {
                    //명령을 다른 메뉴로 이동하려면 "Tools"를
                    //  영어 버전의 메뉴로 변경합니다. 이 코드는 culture를 메뉴 이름에 추가한 다음
                    //  해당 메뉴에 명령을 추가합니다. 모든 최상위 메뉴의 목록을 확인하려면
                    //  CommandBar.resx 파일을 참조하십시오.
                    ResourceManager resourceManager = new ResourceManager("ZAssist.CommandBar", Assembly.GetExecutingAssembly());
                    CultureInfo     cultureInfo     = new System.Globalization.CultureInfo(_applicationObject.LocaleID);
                    string          resourceName    = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
                    toolsMenuName = resourceManager.GetString(resourceName);
                }
                catch
                {
                    //지역화된 버전의 'Tools'를 찾으려고 시도했지만 찾지 못했습니다.
                    //  기본값을 현재 culture에서 작동할 수 있는 en-US로 지정합니다.
                    toolsMenuName = "Tools";
                }

                //도구 메뉴에 명령을 배치합니다.
                //주 메뉴 항목이 모두 들어 있는 최상위 명령 모음인 MenuBar 명령 모음 찾기:
                Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

                //MenuBar 명령 모음에서 도구 명령 모음 찾기:
                CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
                CommandBarPopup   toolsPopup   = (CommandBarPopup)toolsControl;

                //추가 기능에서 처리할 명령을 여러 개 추가하면 이 try/catch 블록이 중복될 수 있습니다.
                //  이 경우 QueryStatus/Exec 메서드도 업데이트하여 새 명령 이름을 포함하기만 하면 됩니다.
                try
                {
                    //명령 컬렉션에 명령 추가:
                    CommandBar toolsMenu = ((CommandBars)(_applicationObject.CommandBars))["Tools"];

                    try
                    {
                        /// 아래 문장에서 catch 로 빠진다는 건 ZAssist 가 메뉴에 없다는 말이다.
                        CommandBarPopup k = (CommandBarPopup)(toolsMenu.Controls["ZAssist"]);
                        k.Caption = k.Caption + "";
                    }
                    catch (ArgumentException)
                    {
                        CommandBarPopup commandBarPopup = (CommandBarPopup)toolsMenu.Controls.Add(MsoControlType.msoControlPopup, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true);

                        subMenu         = commandBarPopup;
                        subMenu.Visible = true;
                        subMenu.Caption = "ZAssist";

                        int iMenuIndex = 1;
                        foreach (NewCommandData newCommData in m_commands)
                        {
                            Command newCommand = null;

                            try
                            {
                                newCommand = commands.Item(_addInInstance.ProgID + "." + newCommData.GetCommandName(), -1);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.Print("이미 존재하는지 검사하는 곳에서 exception 발생 : ", ex.Message);
                            }

                            if (null == newCommand)
                            {
                                newCommand = commands.AddNamedCommand(_addInInstance, newCommData.GetCommandName(), newCommData.GetShowName(), newCommData.GetShowName(), true, newCommData.GetInconIndex(), ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled);
                            }

                            if (newCommand != null)
                            {
                                newCommand.AddControl(subMenu.CommandBar, iMenuIndex);
                            }

                            ++iMenuIndex;
                        }
                    }
                }
                catch (System.ArgumentException ex)
                {
                    //이 경우, 같은 이름의 명령이 이미 있기 때문에 예외가 발생할 수
                    //  있습니다. 이 경우 명령을 다시 만들 필요가 없으며 예외를 무시해도
                    //  됩니다.
                    System.Diagnostics.Debug.Print(ex.Message);
                }
            }
        }