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.

Sorry, this blog does not support comments.

I used various blog hosting services since this blog was established in 2005, but unfortunately they turned out to be unreliable in the long term and comment threads were lost in unavoidable transitions. At this time I don't want to enable third-party services for comments since it has become obvious in recent years that these providers invariably monetize information about their visitors and users.

Please use the links in the page footer to get in touch with me. I'm available for conversations on Keybase, Matrix, Mastodon or Twitter, as well as via email.