Adding SmartTag Verbs to an Activity Designer

Link. September 14, 2006. Comments [0]. Posted in: WinFX | Workflow

Here's a small sample on how to add a new Verb and another SmartTag option to your custom Activity in the Windows Workflow Designer. Adding a new context-menu option to your activity is fairly easy and intuitive: You just need to create a new instance of the ActivityDesignerVerb class when your custom ActivityDesigner-derived class in initialized and add it to the Verbs collection.

Adding a new SmartTag option is a little bit more work. SmartTag options are also ActivityDesignerVerb instances; however, configured SmartTags are returned by the SmartTagVerb property as a readonly collection. To add a new one, you need to override this property and return you own collection instead.

 

Here's a small code sample that adds a single new "Save Image" Verb to right-click menu as well as adding a SmartTag for it:

public class CodeFirstSequentialActivityDesigner : SequenceDesigner

{

   // ...

 

   private List<ActivityDesignerVerb> _smartTagVerbs =

      new List<ActivityDesignerVerb>();

 

   /// <summary>

   /// Overide the SmartTagVerbs collection to return our own

   /// </summary>

   protected override ReadOnlyCollection<ActivityDesignerVerb> SmartTagVerbs

   {

      get { return _smartTagVerbs.AsReadOnly(); }

   }

 

   /// <summary>

   /// Override the Initialize method to add

   /// our custom verbs

   /// </summary>

   /// <param name="activity">Activity to associate with this

   /// designer instance</param>

   protected override void Initialize(Activity activity)

   {

      base.Initialize(activity);

 

      // get parent verbs and add them before our own custom verbs

      _smartTagVerbs.AddRange(base.SmartTagVerbs);

 

      ActivityDesignerVerb saveImageVerb = new ActivityDesignerVerb(

         this, DesignerVerbGroup.Misc,

         "Save Image", OnSaveImageVerbSelected

         );

      saveImageVerb.Enabled = true;

      saveImageVerb.Supported = true;

 

      // add an image

      Guid  imageProp = new Guid("B4C2B905-A6D3-4CD6-A91A-5005A02B9676");

      saveImageVerb.Properties[imageProp] =

         Properties.Resources.Resource_Bitmap.ToBitmap();

      // add verb to both verb collections

      _smartTagVerbs.Add(saveImageVerb);

      Verbs.Add(saveImageVerb);

   }

 

   private void OnSaveImageVerbSelected(object sender, EventArgs e)

   {

      string name = Activity.Name;

      Bitmap emptyImage = new Bitmap(Size.Width, Size.Height,

         PixelFormat.Format32bppArgb);

      using ( Graphics graphics = Graphics.FromImage(emptyImage) )

      {

         Image ourReflection = this.GetPreviewImage(graphics);

         using ( SaveFileDialog dlg = new SaveFileDialog() )

         {

            dlg.Filter = "Image File (*.png)|*.png";

            DialogResult res = dlg.ShowDialog();

            if ( res == DialogResult.OK )

            {

               ourReflection.Save(dlg.FileName, ImageFormat.Png);

            }

         }

      }

   }

}

 



Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Links

Categories

Statistics

Total Posts: 1022
This Year: 92
This Month: 11
This Week: 0
Comments: 792

Blogroll

Post Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.1.8139.823

Sign In