/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary> /// <param term='commandName'>The name of the command to execute.</param> /// <param term='executeOption'>Describes how the command should be run.</param> /// <param term='varIn'>Parameters passed from the caller to the command handler.</param> /// <param term='varOut'>Parameters passed from the command handler to the caller.</param> /// <param term='handled'>Informs the caller if the command was handled or not.</param> /// <seealso class='Exec' /> public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if (commandName == "NAntAddin.Connect.NAntAddin") { ////////////////////////////////////////////////////////////////////////// // If view doesn't yet exists, create a new one if (_addInWindow == null) { object addInView = null; // Create the AddIn View _addInWindow = AddInUtils.CreateAddinView( _applicationObject, _addInInstance, ref addInView, "{01E76687-72C6-4209-85A8-49D0F447E0BE}", "NAntAddin", typeof(NAntAddinView), Properties.Resources.ant); // If view sucessfully create, just initialize it if (addInView != null) { // Initialize the view _addInView = addInView as NAntAddinView; _addInView.ViewController.ApplicationObject = _applicationObject; // Keep a SolutionEvents instance not garbagable _solutionEvents = _applicationObject.Events.SolutionEvents; // And install listeners to solution opened/closed events _solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(_addInView.OnSolutionOpened); _solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(_addInView.OnSolutionClosed); // If a solution is already opened, just simulate opening the solution if (_applicationObject.Solution.IsOpen) { _addInView.OnSolutionOpened(); } } } // Always display the AddIn if NAntAddin is selected on menu _addInWindow.Activate(); ////////////////////////////////////////////////////////////////////////// handled = true; return; } } }
////////////////////////////////////////////////////////////////////////// /// <summary> /// Create a view of the addin /// The view should be a UserControl which will be created directly /// embedded within the Window representing the AddIn in VisualStudio. /// the Window container of the Addin on screee, /// </summary> /// <param name="addInApplication"></param> /// <param name="addInInstance"></param> /// <param name="addInView"></param> /// <param name="viewGuid"></param> /// <param name="viewCaption"></param> /// <param name="viewClass"></param> /// <param name="viewIcon"></param> /// <returns></returns> ////////////////////////////////////////////////////////////////////////// public static Window CreateAddinView(DTE2 addInApplication, AddIn addInInstance, ref object addInView, string viewGuid, string viewCaption, Type viewClass, Bitmap viewIcon) { Windows2 dteWindows = (Windows2)addInApplication.Windows; Window addInWindow = dteWindows.CreateToolWindow2(addInInstance, viewClass.Assembly.Location, viewClass.FullName, viewCaption, viewGuid, ref addInView); if (addInWindow != null) { // Set the tab icon if (viewIcon != null) { StdPicture tabPicture = AddInUtils.CreatePicture(viewIcon); addInWindow.SetTabPicture(tabPicture); } addInWindow.Linkable = true; addInWindow.IsFloating = false; addInWindow.Activate(); } return(addInWindow); }
/// <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_UISetup) { object [] contextGUIDS = new object[] { }; Commands2 commands = (Commands2)_applicationObject.Commands; 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("NAntAddin.CommandBar", Assembly.GetExecutingAssembly()); CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID); if (cultureInfo.TwoLetterISOLanguageName == "zh") { System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent; resourceName = String.Concat(parentCultureInfo.Name, "Tools"); } else { resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"); } 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 = "Tools"; } //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: Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"]; //Find the Tools command bar on the MenuBar command bar: CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl; //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 { //Add a command to the Commands collection: Command command = commands.AddNamedCommand2(_addInInstance, "NAntAddin", "NAntAddin", "Executes the command for NAntAddin", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); //Add a control for the command to the tools menu: if ((command != null) && (toolsPopup != null)) { //command.AddControl(toolsPopup.CommandBar, 1); ////////////////////////////////////////////////////////////////////////// CommandBarButton addinButon = (CommandBarButton)command.AddControl(toolsPopup.CommandBar, 1); addinButon.Picture = AddInUtils.CreatePicture(Properties.Resources.ant); addinButon.Mask = AddInUtils.CreatePicture(Properties.Resources.ant_mask); ////////////////////////////////////////////////////////////////////////// } } 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. } } }