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

有帮助吗?

解决方案

Try this in your renderer class:

public class MainFormToolStripRenderer : ToolStripProfessionalRenderer {

  public MainFormToolStripRenderer() {
    this.RoundedEdges = false;
  }
}

其他提示

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

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top