2

I'm writing some custom behavior for a ToolStripDropDown control. I'd also like to modify the ToolStripDropDownButton itself to display a colored shape.

I see that I can handle the Paint event and draw whatever I like. However, is there any way to have the button paint the default background before I paint the shape? It would be hard to get the background exactly right, especially with future versions of .NET and Windows.

In plain ol' Windows, I could invoke the default proc handler before or after my paint code. I'm not seeing any way to accomplish that in .NET. Or perhaps there's a way to tell the button to paint only the background?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

1 Answers1

2

When you handle the Paint event (as opposed to overriding the OnPaint method in a derived class) the base class (default proc handler) is already getting called. Everything gets drawn as normal, and then you're essentially drawing on top of that in the Paint event. You can see that clearly here:

     Lime rectangle drawn over part of the drop-down button, clearly showing the default contents underneath

The trick is making sure that you leave enough of the control's clipping rectangle exposed to show the part you want. The e.ClipRectangle property retrieves the entire button's client area, so if you just
fill that with a color swatch, you're going to cover up the drop-down arrow and default background, too. The above demonstration was created using the following ugly sample code:

private void ToolStripDropDownButton1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.Chartreuse,
                             e.ClipRectangle.X + 3, e.ClipRectangle.Y + 3,
                             e.ClipRectangle.Width - 12,
                             e.ClipRectangle.Height - 12);
}

Other than that, I don't think there's a way to customize what exactly gets drawn by the base class. Owner-drawing (at least in WinForms) tends to be an all-or-nothing affair. You get complete control,
but it comes at the price of having to implement everything yourself.


Of course, in case you haven't already noticed, the ToolStrip control already doesn't look like a native Windows control. And even worse, it is always going to look exactly the same as it does now,
even in future versions of Windows that completely overhaul the UI. (The MenuStrip is plagued by
this same phenomenon, and the difference is very visible in Windows Vista/7 where the standard API menus have changed dramatically). The reason is that both controls are drawn entirely in C# code written in their WinForms implementations. Personally, I think it looks ridiculously cheesy, and wouldn't use it in one of my applications on a bet.

You can assign a custom renderer that uses the UxTheme API to draw the buttons, which will get much closer to approximating the look of the native menus and toolbars. A pretty thorough sample is available here. I've written something very similar for the WinForms development that I've done requiring the additional features of the ToolStrip class (such as embedding combo boxes) not offered by the
old-school MainMenu and ToolBar controls that simply wrap their Windows API equivalents. By choosing to do things this way, you do have more control over exactly what parts of the base class renderer you wish to call, as you've written the code explicitly yourself. Highly recommended if you're
the type that cares at all about UI, native feel, or user experience.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks for the detailed response. I'm bothered by having the button paint itself entirely before I start my own painting. I found some interesting code [here](http://en.csharp-online.net/Tool,_Menu,_and_Status_Strips%E2%80%94Creating_an_Owner-Drawn_ToolStripItem) that tells the button to draw only the background. But it appears that only works with a UserControl (I have a custom control). (The UxTheme stuff looks interesting but it probably not needed for this project.) – Jonathan Wood Feb 19 '11 at 05:48
  • @Jonathan: I don't know where you got the idea that's a UserControl. It looks to me like they're simply sub-classing the `ToolStripButton` class. In doing so, you can override the `OnPaint` method instead (as I mentioned) and choose not to call the base class's default renderer. Instead, you can get access to individual methods of the renderer (such as `DrawButtonBackground`) and call those separately. That's pretty much the same thing as I discussed in the last portion of my answer. You're just getting the cheesy `ToolStrip` look, rather than the native look. – Cody Gray - on strike Feb 19 '11 at 05:57
  • Remember that the `ToolStrip` controls all use a [`ToolStripRenderer`](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstriprenderer.aspx) object to handle the painting functionality. That's why you can choose between the "Professional" and "System" styles ("System" looks nothing like native, at least in Vista/7), because there are two derived classes (`ToolStripProfessionalRenderer` and `ToolStripSystemRenderer`) that implement separate painting routines for those two styles. Subclassing the control allows you to get access to a protected property exposing its render. – Cody Gray - on strike Feb 19 '11 at 05:59
  • @Cody: Sorry, I'm juggling several articles. The link I referenced does discuss inheriting directly from `ToolStripButton`, and if I take that road I just can't get it to show up in the designer. Guess I've got a few issues going on. Thanks for all the excellent info. – Jonathan Wood Feb 19 '11 at 06:03
  • @Jonathan: Understandable. If you're really not interested in customizing the underlying rendering, I'm not sure why you care that the button is already painted by the base class before you do your own painting. I seriously doubt that you'll see any performance issues. The simplest way is accounting for the size of a drop-down arrow and limiting your drawing to only that region. There are all sorts of complicated workarounds, but they each have their drawbacks. (Although, I swear I've gotten subclassed `ToolStripButton` controls to show up in the designer. Make sure it's declared `public`.) – Cody Gray - on strike Feb 19 '11 at 06:08
  • @Cody: It's a combination of performance and concerns about requiring the control user to provide an image at the same time I must ensure my code doesn't allow that image to show. Doesn't seem quite right to me. That article I referenced showed their control in the designer. Guess I'm missing something. – Jonathan Wood Feb 19 '11 at 06:16