Declarative Visual Studio addin buttons with icons

in the previous post I said how I hated doing custom icons for addins and showed how you can make it work. In this post I’ll show what I feel is a better way of managing the actions on the buttons, and how to declaratively create buttons with icons that are loaded automatically.

First, You can download the source here. What you’ll find there are a bunch of helper classes and a base class that allows you to declare an addin button like this:

    [AddinButtonCaption("yo","some description")]

    [AddinButtonIconName("boldhs.bmp")]

    [AddinUnderMenuName("MenuBar","Tools")]

    public class HelloWorldAddinAction:AddinActionBase

    {

        public HelloWorldAddinAction(CommandBarButton relatedButton)

            : base(relatedButton){}

 

        public override void Execute()

        {

            MessageBox.Show("hello there!");

        }

    }

You set the caption, icon and location of the addin button using the attributes on top of the class. You need to inherit from AddinActionBase and override the Execute() method to do anything useful. You also get access to the related button to change its properties later on.  The icon path is hard coded to be the location of the running assembly\icons.

here is another button that switches icons and text and is located under the “Test” menu:

 

    [AddinButtonCaption("Another Button!","and descccc")]

    [AddinButtonIconName("book_openhs.bmp")]

    [AddinUnderMenuName("MenuBar","Test")]

    class ActionThatReplacesItsIcon:AddinActionBase

    {

        private bool isOn = false;

        public ActionThatReplacesItsIcon(CommandBarButton relatedButton)

            : base(relatedButton)

        {

        }

 

        public override void Execute()

        {

            MessageBox.Show("Another one!");

            isOn = !isOn;

            if(!isOn)

                SetButtonIcon("book_anglehs.bmp");

            else

                SetButtonIcon("book_openhs.bmp");

 

            RelatedButton.Caption = isOn.ToString();

 

        }

    }

It uses the “RelatedButton” property to set caption on the button, and uses a derived method “SetButtonIcon” to change icons.

 

So – how do you initialize all these buttons? in the “OnConnection” method, you call a special “AddinReflector” class that does all the work for you:

        private DTE2 _applicationObject;

        private AddIn _addInInstance;

 

        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)

            {

                Commands2 commands = (Commands2)_applicationObject.Commands;

                AddinReflector.LoadAllButtonsInCurrentAssembly(commands, _addInInstance,_applicationObject);

            }

        }

 

You need override another method to make the methods execute correctly:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

        {

            handled = false;

            if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)

            {

                AddinActionBase action = AddinRegistry.GetActionByName(commandName);

                action.Execute();

                handled=true;

            }

        }

 

The idea is simplicity: you want to add another button? with an icon? just create a simple class with some attributes and you’re done.

You can download the source here

  • Icons are hard coded to be found under [current assembly location]\icons
  • the classes need to be declared in the current assembly (easily changed if you’d like)
Published Saturday, August 02, 2008 4:21 PM by RoyOsherove

Comments

Saturday, August 02, 2008 5:54 PM by Ran Davidovitz

# re: Declarative Visual Studio addin buttons with icons

While reading your previous post I started doing the same code :).

You are fast - Great work!, Thanks for sharing

Saturday, August 02, 2008 5:55 PM by Omer Rauchwerger

# re: Declarative Visual Studio addin buttons with icons

Thanks Roy,

This will help reduce button bureaucracy in future add-ins.

-- rauchy

Sunday, August 03, 2008 3:50 AM by Anthony Bouch

# re: Declarative Visual Studio addin buttons with icons

Very cool. Have battled with addin button icons and menu positions in the past. At one point I had to write an ugly hack because there were "two" commandbars with the name "Project".

# Dew Drop - August 3, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - August 3, 2008 | Alvin Ashcraft's Morning Dew

Sunday, August 03, 2008 7:05 PM by ISerializable - Roy Osherove's Blog

# VS Addins Revisited: Declarative button order and location with a DSL

First, here is the code with the updates . In the previous post I showed how you can declaratively create

Sunday, August 03, 2008 7:50 PM by Community Blogs

# Interesting Finds: 2008.08.02~2008.08.04

.NET Loading static content in ASP.NET pages from different domain for faster parallel download Declarative

Sunday, August 03, 2008 7:50 PM by Community Blogs

# VS Addins Revisited: Declarative button order and location with a DSL

First, here is the code with the updates . In the previous post I showed how you can declaratively create

Monday, August 04, 2008 6:37 AM by Daniel Pihlström

# re: Declarative Visual Studio addin buttons with icons

I like it, though personally I have a lot more problems with trying to get rid of things in my menus, rather than adding to them. :)

Tuesday, August 05, 2008 11:12 AM by Visual Studio Hacks

# Visual Studio Links #61

My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. Chris Craft created a series of posts in July he called 31 Days of Visual Studio Tips & Tricks. All of the posts along with the

Wednesday, August 06, 2008 1:23 AM by Declarative Visual Studio addin buttons with icons ·

# Declarative Visual Studio addin buttons with icons ·

Pingback from  Declarative Visual Studio addin buttons with icons ·

Thursday, February 12, 2009 3:20 AM by Lazy programming

# Attach to process for lazies

Attaching to a process is something that developers use very often to debug, but what happens when the