Overriding OnPaint in ToolStrip

Hey there,

When trying to implement custom background painting in a ToolStrip control (System.Windows.Forms.ToolStrip), I find myself having to add a bit of hack design. When the ToolStrip control's RenderMode property is System, there is always a grey line drawn at the bottom edge of the control. When RenderMode is instead set to Professional, there is a blueish thinner rounded line at the bottom and right edges.
 
These lines seem to be drawn after the overridden OnPaint method and I can't get the lines to disappear without adding a bit of hack code.
 
The ugly solution: I put the ToolStrip control within a Panel, set both controls' AutoSize property to False and derived a new class from LayoutEngine to "manually" resize the controls so that the ToolStrip always is a couple of pixels higher than the Panel. (Badness, badness!!!) Using a Padding.Bottom value of 2 for the ToolStrip control, the ToolStrip contents look as usual, my painting code in the overridden OnPaint method works fine, and the grey line is clipped out by the slightly smaller Panel control.
 
This is NOT good practice, I know, and I'd love to do this "the right way" instead, but I just can't think of another way of doing this.

Does anybody have a suggestion for me Please



Answer this question

Overriding OnPaint in ToolStrip

  • Jon vdB

    Thanks for your great advice! That's exactly what I needed. The Renderer classes are something that I have totally missed so far. That opens up for a whole new level of customized paintwork. Smile

  • Jerry017

    All the paint in the ToolStrip goes through it's ToolStripRenderer class.  You can inherit from the System or ProfessionalRenderers and tweak most parts of the painting.

    ToolStrip.Renderer = new MyFancyRenderer();

    public class MyFancyRenderer : ToolStripProfessionalRenderer {

       protected override OnRenderToolStripBorder(ToolStripItemRenderEventArgs e) {
          // dont call base.
       }
    }

    More samples:
    http://msdn2.microsoft.com/en-us/library/ms181005

  • Overriding OnPaint in ToolStrip