Click here to Skip to main content
12,398,225 members (53,490 online)
Click here to Skip to main content

Stats

39.5K views
850 downloads
42 bookmarked
Posted

Visual Studio Add-in Library

, 15 Sep 2010 CPOL
This library allows you to quickly and simply create add-ins for VS2008 and VS2010 (VS2005?).
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 EnvDTE;
using VisualStudio.AddInLibrary;
using VisualStudio.AddInLibrary.Helpers;
using VisualStudio.AddInLibrary.Menus;
using System.Windows.Forms;

namespace TestAddin
{
  class ToolsMenu : BaseMenu, IMenuImplementation
  {

    public ToolsMenu(BaseMenuParams _params)
      : base(_params)
    {

    }
   
    #region IMenuImplementation Members

    public void CreateCommands()
    {
      ClearCommands(this);
      AddCommand(new VSCommandInfo()
      {
        Name = "NEW",
        Owner = this,
        HaveSeparator = false,
        Caption = "New",
        Position = 1,
        ToolTip = "",
        BitmapResourceId = 2,
        UseOfficeResources = false,
        Action = new PerformActionForVSCommandDelegate(RunCommand_New)
      });
      AddCommand(new VSCommandInfo()
      {
        Name = "EDIT",
        Owner = this,
        HaveSeparator = false,
        Caption = "Edit",
        Position = 2,
        ToolTip = "",
        BitmapResourceId = 59,
        UseOfficeResources = true
      });
    }

    /// <summary>
    /// Creates main Menu and subMenus
    /// </summary>
    public void Install()
    {
      // build and store commands
      CreateCommands();

      // create our menu with associated commands
      try
      {
        AddMenu(this, // the class instance
                "Tools",  // the ENGLISH Visual Studio Menu / Command we want to attach too
                true, // flag telling base class that we attach to main menu (File,Edit ...)
                ResourceMANAGER.GetString("MY_NAME"), //  Our Main Menu name ... here extracted from plugin resources
                MenuType.Popup, // Popup = create a menu with each commands as submenus under choosed VS menu
                1, // position or our main menu
                true); // true = add separator
      }
      catch (Exception ex)
      {
        System.Windows.Forms.MessageBox.Show(ex.Message);
      }
    }

    /// <summary>
    /// Remove menu and submenus
    /// </summary>
    public void UnInstall()
    {
      RemoveMenu(this);
    }

    /// <summary>
    /// Returns information about a command to VS
    /// </summary>
    /// <param name="command">command name</param>
    /// <param name="NeededText">VS query status</param>
    /// <param name="CommandText">command caption</param>
    /// <returns></returns>
    public MenuState GetState(string command, vsCommandStatusTextWanted NeededText, ref object CommandText)
    {
      MenuState m = MenuState.Enabled;

      bool bsupport = VStudioHelpers.IsActiveProjectSupported(VStudioApplication);
      /* --------------- INFO --------------------------
       * bsupport will be set to true if when running the plugin 
       * - a csharp project is opened
       * - a Web or Winform application project is opened
      ---------------------------------------------*/

      // translate VS command in "readable" status
      QueryMenuVSCommandStatus status = TranslateCommandState(NeededText);

      // perform action ...
      switch (status)
      {
        // 2: need to change command caption ?
        case QueryMenuVSCommandStatus.Step2_GetText:
          if (!bsupport)
          {
            VSCommandInfo cmd = GetCommand(command);
            if (cmd != null)
              CommandText = cmd.Caption + " - NOT SUPPORTED";
            else
              CommandText = "NOT SUPPORTED";
          }
          break;

        // 3 : do we enable/disable/hide the command in the menu ?
        case QueryMenuVSCommandStatus.Step3_FinalState:
          if (!bsupport)
            m = MenuState.Disabled;
          else
            m = MenuState.Enabled;
          break;

      }
      return m;
    }

    /// <summary>
    /// Does this command belongs to this instance ?
    /// </summary>
    /// <param name="command">command</param>
    /// <returns></returns>
    public bool IsItMycommand(string command)
    {
      return IsItMyCommand(this, command);
    }

    /// <summary>
    /// Code associated with command "NEW"
    /// </summary>
    private void RunCommand_New()
    {
      // do what you want after running custom actions
      bool bSolutionLoaded = VStudioHelpers.isSolutionLoaded(VStudioApplication);
      if (bSolutionLoaded)
      {
        ProjectType pfound = null;
        EnvDTE.Project p = VStudioHelpers.GetActiveProject(VStudioApplication);
        if (p != null)
        {
          if (VStudioHelpers.IsSupportedProject(p, out pfound))
          {
           MessageBox.Show("NEW");
          }
          else
           MessageBox.Show("Unsupported Project !");
        }
        else
         MessageBox.Show("No project loaded !");
      }
      else
       MessageBox.Show("No Solution loaded !");
    }
   

    #endregion
  }
}

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

You may also be interested in...

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