Search code examples
c#toolstrip

Change the BackColor of the ToolStripSeparator control


Is it possible to change the BackColor of ToolStripSeparator control? There is a BackColor property in the designer, but it doesn't appear to be used - the color is always white.


Solution

  • The default toolstrip renderer ignores the BackColor property and uses hard-coded colors.

    You can refer following link to use your own renderer to paint the separators the way you want them.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            toolStrip1.Renderer = new MyRenderer();
        }
        
        private class MyRenderer : ToolStripProfessionalRenderer
        {
            protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
            {
                if ((e.Item as ToolStripSeparator) == null)
                {
                    base.OnRenderSeparator(e);
                    return;
                }
                Rectangle bounds = new Rectangle(Point.Empty, e.Item.Size);
                bounds.Y += 3;
                bounds.Height = Math.Max(0, bounds.Height - 6);
                if (bounds.Height >= 4)
                    bounds.Inflate(0, -2);
                int x = bounds.Width / 2;
                using(Pen pen = new Pen(Color.DarkBlue))
                    e.Graphics.DrawLine(pen, x, bounds.Top, x, bounds.Bottom - 1);
                using (Pen pen = new Pen(Color.Blue))
                    e.Graphics.DrawLine(pen, x + 1, bounds.Top + 1, x + 1, bounds.Bottom);
            }
        }
    }
    

    Source: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6cceab5b-7e06-40cf-82da-56cdcc57eb5d