Changing the default icon of a custom workflow activity

By default every custom workflow activity shows the same icon. And using an icon that help understand what the activity actually does makes life so much easier for the user of your activity. And remember he is a developer so some day he might just return the favor [:)]

 

The default looks like this.

image

 

So changing the icon isn’t hard there are just a few steps that might catch you if you aren’t careful.

This is what my demo project looks like:

image

 

  1. Add the new image to the project. In my case I added a folder named Images and dropped the PushpinHS.png in there. This PushpinHS.png can be found in the standard VS2008ImageLibrary.zip located in C:\Program Files\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033.
  2. Change the build action for PushpinHS.png to Embedded Resource.
  3. Next go to the Activity1.cs file and add the ToolboxBitmap attribute to the Activity1 class. We need to specify two parameters, the first is a type in which assembly the PushpinHS.png is located and the second is the name of the resource to use. These two are related because the resource name us relative to the namespace of the type used.
  4. [ToolboxBitmap(typeof(Activity1), "Images.PushpinHS.png")]
    public partial class Activity1: SequenceActivity
    {

    }

And now we have a nice new icon in the activity like this:

image

 

One problem is that the resource name is actually relative to the class specified. Using relfector makes it easy to see all the actual names, including the namespace used.

image

 

So what to do if your activity is in another namespace and a direct reference to the resource isn’t possible?

The easiest way is to add another class outside of all namespaces and fully specify the resource name like this:

using System.Drawing;
using System.Workflow.Activities;

internal class ResourceLoader
{ }

namespace WorkflowConsoleApplication1.MyActivities
{
    [ToolboxBitmap(typeof(ResourceLoader), "WorkflowConsoleApplication1.Images.PushpinHS.png")]
    public partial class Activity1: SequenceActivity
    {
        public Activity1()
        {
            InitializeComponent();
        }
    }
}

 

Note the ResourceLoader used in the ToolboxBitmap attribute is not included in any namespace to we need to specify the full resource name “WorkflowConsoleApplication1.Images.PushpinHS.png”.

 

Enjoy!

One thought on “Changing the default icon of a custom workflow activity

  1. Do you know if there is a way of controlling the size of the activity images?

    I’m overriding the Image property of the Activity class to change the image, but when the activity is rendered on the design surface, the image appears to have been downscaled considerably from it’s actual size.

    Cheers,

    Jim

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>