示例#1
0
        //////////////////////////////////////////////////////////////////////////
        /// <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);
        }
示例#2
0
        /// <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.
                }
            }
        }