11

I am working on a Windows Form app (C#, .NET 4.0, VS 2010), where I have a pretty standard MainForm with a ToolStrip (GripStyle: Hidden, Dock: Top, RenderMode: ManagerRenderMode). The toolstrip contains a few basic items (ToolStripLabel, ToolStripSeparator, ToolStripSplitButton).

This is rendered as follows:

ToolStrip rendered by default ManagerRenderMode

At first I simply wanted to add a 'bottom' border below the toolstrip, but I also noticed that this toolstrip is rendered with 'rounded corners' (you can see the right-hand-side top and bottom ones in the image), and a vertical gradient line.

How can I make these corners NOT rounded?

I tried:

public class MainFormToolStripRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        base.OnRenderToolStripBorder(e);

        var y = e.ToolStrip.Height-1;
        e.Graphics.DrawLine(new Pen(SystemColors.ControlDark, 1), new Point(0, y), new Point(e.ToolStrip.Width, y));
    }

And wired it up via this.toolStrip_Actions.Renderer=new MainFormToolStripRenderer(); in my form initialization.

This gave me the bottom border, but didn't do anything for the rounded corners. Also, with the added bottom border, the rounded corners are more noticeable:

ToolStrip rendered by custom ToolStripProfessionalRenderer

Next I tried drawing a rectangle during the same event handler above, to try (at least) to hide the rounded corners and vertical gradient behind a solid rectangular border. That didn't work because the available drawing area (e.AffectedBounds) is within the rounded borders.

I also tried to set the ToolStrip's RenderMode to System (and not use my renderer). In this case the toolstrip corners seem to fit snugly (rectangular), BUT the splitbutton within the toolbar seems to be broken (clicking the down arrow does not display the dropdown), for as-yet-unknown reasons, and the overall look-n-feel is a bit underwhelming (quite flat, until you hover on some buttons in the toolstrip).

I guess in the end I'd rather stick with the ManageeRenderedMode, or a custom renderer inheriting from the Professional one - but I need to get rid of the rounded corners. Among others, I found this SO Q which seems to point close to what I'm looking at but didn't give me an answer for my case.

Thanks in advance

Community
  • 1
  • 1
FOR
  • 4,260
  • 2
  • 25
  • 36
  • 1
    Winforms is a pain for rendering something different without creating your own control entirely. It may be too late now, but have you messed with WPF/XAML? – iMortalitySX Nov 16 '12 at 14:40
  • Thanks, but we looked at WPF earlier and for now we're trying to stick with "basic" winforms. I don't mind inheriting from the standard Toolstrip if I need to override certain details, but yeah: it'd be nice not to have to render the whole thing on our own (especially with the splitbutton we need inside of it, etc etc) – FOR Nov 16 '12 at 14:45

3 Answers3

11

Try this in your renderer class:

public class MainFormToolStripRenderer : ToolStripProfessionalRenderer {

  public MainFormToolStripRenderer() {
    this.RoundedEdges = false;
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • awesome! Thank you (I actually set the .RoundedEdges property to false in my custom renderer, but that was the trick)! Still having a bit of trouble because now that the corners are not rounded the split button stopped working... but I'll accept your answer since it solved the primary issue and put me on the right path. – FOR Nov 16 '12 at 14:47
  • 3
    actually, there is a RenderMode option in the ToolStrip properties which has 3 options: System, Professional, and ManagerRenderMode. The first option let the OS render the ToolStrip which is NOT rounded, and the last two is rounded – am05mhz Jan 14 '16 at 02:39
4

Building on the accepted answer by LarsTech, you don't necessarily need to implement a new Renderer class, unless there are compelling reasons to do so.

You can do this as a one liner as follows:

toolStrip_Actions.Renderer = new ToolStripProfessionalRenderer() { RoundedEdges = false };

or since the default renderer for a ToolStrip with RenderMode set to ManagerRenderMode is already a ToolStripProfessionalRenderer, you may cast it as such and access the RoundedEdges property directly as follows:

((ToolStripProfessionalRenderer)toolStrip_Actions.Renderer).RoundedEdges = false;
warren.sentient
  • 513
  • 6
  • 10
3

As am05mhz mentioned, just select RenderMode > System and the rounded corners disappear:

Floern
  • 33,559
  • 24
  • 104
  • 119
Adrian Smith
  • 1,013
  • 1
  • 13
  • 21