Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toolStrip to have menuStrip gradient background

I have a form with a menu and a toolstrip at the top. The menuStrip has a nice looking gradient background, how can I get the same effect on the toolStrip control? I know about the RenderMode property but changing this doesn't have the desired result.

toolStrip background

like image 838
PaulK Avatar asked Nov 12 '22 22:11

PaulK


1 Answers

You can achieve this with a custom renderer.

public class CustomToolStripRenderer : ToolStripProfessionalRenderer
{
    public CustomToolStripRenderer() { }

    protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
    {
        //you may want to change this based on the toolstrip's dock or layout style
        LinearGradientMode mode = LinearGradientMode.Horizontal;

        using (LinearGradientBrush b = new LinearGradientBrush(e.AffectedBounds, ColorTable.MenuStripGradientBegin, ColorTable.MenuStripGradientEnd, mode))
        {
            e.Graphics.FillRectangle(b, e.AffectedBounds);
        }
    }
}

Then set your toolstrip to use an instance of this renderer.

public Form1()
{
    InitializeComponent();

    CustomToolStripRenderer r = new CustomToolStripRenderer();
    r.RoundedEdges = false;

    toolStrip1.Renderer = r;
}
like image 162
gannaway Avatar answered Nov 14 '22 21:11

gannaway