Click here to Skip to main content
11,496,146 members (383 online)
Click here to Skip to main content
Add your own
alternative version

Visual Studio Add-in Library

, 15 Sep 2010 CPOL 35K 808 41
This library allows you to quickly and simply create add-ins for VS2008 and VS2010 (VS2005?).
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
VSAddInLibrary_src.zip
TestAddin
Resources
1.ico
2.bmp
serviceunknown.ico
TestAddin.AddIn
TestAddin.csproj.user
VisualStudio Add-in Library Sources
Helpers
Menus
Actions
Base
Properties
VisualStudio Add-in Library.csproj.user
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using VisualStudio.AddInLibrary.Menus;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using stdole;

namespace VisualStudio.AddInLibrary
{
  [ComVisible(true)]
  [ClassInterface(ClassInterfaceType.None)]
  public class CommandManager : IDispatch, IDTCommandTarget, IDTExtensibility2
  {
    /// <summary>
    /// The client assembly <---> the plugin
    /// </summary>
    protected Assembly PluginAssembly = null;


    /// <summary>
    /// Internal list containing our command class instances
    /// </summary>
    List<IMenuImplementation> MenuList= new List<IMenuImplementation>();


    public void AddCommand(IMenuImplementation menu)
    {
      MenuList.Add(menu);
    }

    /// <summary>
    /// Creates and install Menu /sub-menus
    /// </summary>
    public void InstallMenus()
    {
      foreach (IMenuImplementation imp in MenuList)
      {
        imp.Install();
      }  
    }

    /// <summary>
    /// Remove menus & submenus
    /// </summary>
    public void UnInstallMenus()
    {
      foreach (IMenuImplementation imp in MenuList)
      {
        imp.UnInstall();
      }
      MenuList.Clear();
    }

    /// <summary>
    /// User/Plugin Method called to create the menus and their commands
    /// </summary>
    protected virtual void CreateCommands()
    {
      throw new Exception("NOT SUPPORTED - CreateCommands must be implemented in derived class !");
    }   


    #region IDTCommandTarget Members

    public void Exec(string CmdName, vsCommandExecOption ExecuteOption, ref object VariantIn, ref object VariantOut, ref bool Handled)
    {
      bool ret = false;
      foreach (IMenuImplementation imp in MenuList)
      {
        if (imp.IsItMycommand(CmdName))
        {
          ret = imp.Exec(CmdName);
          if (ret) break;
        }
      }
      Handled =  ret;
    }

    public void QueryStatus(string CmdName, vsCommandStatusTextWanted NeededText, ref vsCommandStatus StatusOption, ref object CommandText)
    {
      StatusOption = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
      foreach (IMenuImplementation imp in MenuList)
      {
        // to whom this command belongs too ...
        if (imp.IsItMycommand(CmdName))
        {
          MenuState m = imp.GetState(CmdName, NeededText, ref CommandText);
          switch (m)
          {
            case MenuState.Enabled:
              StatusOption = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
              break;

            case MenuState.Disabled:
              StatusOption = vsCommandStatus.vsCommandStatusSupported;
              break;

            case MenuState.Hidden:
              StatusOption = vsCommandStatus.vsCommandStatusInvisible;
              break;

            case MenuState.Unknown:
            case MenuState.Unapplicable:
              StatusOption = vsCommandStatus.vsCommandStatusSupported;
              break;
          }
          break;
        }
      }            
    }

    #endregion


    #region IDTExtensibility2 Members

    /// <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 virtual void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
    {
      UnInstallMenus();
    }

    /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />		
    public virtual void OnAddInsUpdate(ref Array custom)
    {
    }

    /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public virtual void OnStartupComplete(ref Array custom)
    {
      InstallMenus();

    }

    /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public virtual void OnBeginShutdown(ref Array custom)
    {
    }

    /// <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)
    {
      try
      {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

        if (connectMode == ext_ConnectMode.ext_cm_Startup)
        {
          // call derived class methods
          CreateCommands();          
        }

        if (connectMode == ext_ConnectMode.ext_cm_AfterStartup)
          InstallMenus();

      }
      catch (System.Exception e)
      {
        System.Windows.Forms.MessageBox.Show(e.ToString());
      }

    }
    #endregion


    protected DTE2 _applicationObject = null;
    protected AddIn _addInInstance = null;

   

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

KL@DotNet
Program Manager Digitas
France France
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Sep 2010
Article Copyright 2010 by KL@DotNet
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid