Custom drawing bar items

I’m using Developer Express XtraBars in my .NET applications. With one of the latest releases, XtraBars also support the new skinning technology, which the website depicts quite inadequately but which takes the experience of an application based completely on DX controls to a new level visually.

Now who would have thought that a package such as this would have grave deficiencies in other areas? Well, custom drawing is one such area. Recently I wanted to create a simple bar item that would be able to specify its own background and foreground colours, independent of the configured paint style/skinning. I played with deriving my own BarItem, BarItemLink, BarLinkViewInfo, PrimitivesPainter, PaintStyle and several other classes for the larger part of a day, until I finally decided that the current structure of the whole XtraBars painting is such that this simple modification is enormously complicated, IF I want to stay within the extensibility structure that’s available out of the box.

After spending a lot of time with these efforts, I had quite a good understanding of how the painting really works, and I went on to extend XtraBars with a simple event on the BarItem that lets me do custom drawing in an event handler. If you want to use these changes, you can download (or look at) a standard patch file here, but there are a few things to consider:

  1. I use VS 2005 and .NET 2, therefore the patch uses the generic EventHandler<T> delegate. You’ll have to replace that with a non-generic delegate if you want to use this for VS 2003.
  2. You’ll only be able to use this if you compile your XtraBars source code yourself.

If these points are no problems to you, you’ll be able to use code like this in an event handler to draw a bar item link yourself:

private void barButtonItem1_DrawLink(object sender, DrawLinkEventArgs e) {
  BarLinkState st = e.BarLinkPaintArgs.LinkInfo.CalcRealPaintState( );
  if (st == BarLinkState.Normal) {
    e.BarLinkPaintArgs.Graphics.FillRectangle(
      e.BarLinkPaintArgs.Cache.GetSolidBrush(Color.Yellow), 
      e.BarLinkPaintArgs.LinkInfo.Bounds);
    e.BarLinkPainter.PPainter.DrawLinkCaption(e.BarLinkPaintArgs, 
      e.BarLinkPaintArgs.Cache.GetSolidBrush(Color.Red),
      BarLinkState.Normal);
    e.Handled = true;
  }
}

This code would result in an appearance similar to this, regardless of the paint style used:

Obviously I sent this patch to support together with my wish for a similar feature in the box, and I got the reply that custom drawing was an item in their list of things to do. Not a completely new piece of information… anyway, if you want to use it now, consider going a similar way as I did.

3 Comments on Custom drawing bar items

  1. thank you for you effort but i want to know how can i deal with patch file
    in vb.net 2003
    thank you in advance

    Like

  2. Hi Rami,

    Unfortunately, Microsoft tools don’t have direct support for standard patch formats (never had and I guess will never have, afaik). To use a patch file, you need a tool called patch (duh!). The easiest way to get hold of it on Windows is to install Cygwin.

    Like

  3. thank you again but is there
    another way to deal with batch File
    and i use .net 2003 how can i
    use your Code

    Like

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s