Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override ToolStripSystemRenderer to change the color of a dropdown backgroud, without changing the toolstrip background color?

Tags:

c#

winforms

I created a class that inherits ToolStripSystemRenderer and have added overriden various events to style my toolstrip the way I want. But I can't seem to change background of a button's dropdown menu, without changing the background color of the toolstrip itself.

Here's an example of the menu. I want to change that system color.

enter image description here

My renderer class is pretty simple at the moment.

public class AvertToolStripRenderer : ToolStripSystemRenderer
{
    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        //base.OnRenderToolStripBorder(e);
    }
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    {
        e.TextColor = Color.Black;
        base.OnRenderItemText(e);
    }
    protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
    {
        e.ArrowColor = Color.White;
        base.OnRenderArrow(e);
    }
}
like image 396
ernest Avatar asked Dec 08 '25 21:12

ernest


1 Answers

You need to set the renderer of your toolstrip(just in case you forgot about this detail):

YourToolstrip.Renderer = new AvertToolStripRenderer();

Next on your custom renderer class:

public class AvertToolStripRenderer : ToolStripProfessionalRenderer
{
    //rest of your implementation...

    protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
    {
        ToolStripDropDown dr = e.ToolStrip as ToolStripDropDown;

        if (dr != null)
        {
            e.Graphics.FillRectangle(Brushes.Beige, e.AffectedBounds);
        }
    }
}
like image 75
terrybozzio Avatar answered Dec 11 '25 11:12

terrybozzio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!