5

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.

Surfbutler
  • 1,529
  • 2
  • 17
  • 38
Siwar
  • 219
  • 2
  • 7
  • 18

3 Answers3

5

I just pointed my separators' Paint event to this custom proc:

    private void mnuToolStripSeparator_Custom_Paint (Object sender, PaintEventArgs e)
    {
        ToolStripSeparator sep = (ToolStripSeparator)sender;

        e.Graphics.FillRectangle(new SolidBrush(CUSTOM_COLOR_BACKGROUND), 0, 0, sep.Width, sep.Height);

        e.Graphics.DrawLine(new Pen(CUSTOM_COLOR_FOREGROUND), 30, sep.Height / 2, sep.Width - 4, sep.Height / 2);

    }

Where the CUSTOM_COLOR_FOREGROUND is a solid/named Color, such as Color.White for example.

Flood
  • 345
  • 7
  • 12
4

I see the question was asked 2 years ago, but I still can't find a simple and clear solution for this on the web. So...

I've just faced the problem today and found that it's pretty simple to solve it.

Having the same situation:

enter image description here

Solution:

Create a class which inherits the ToolStripSeparator class and add a method to the Paint EventHandler to draw the separator:

public class ExtendedToolStripSeparator : ToolStripSeparator
{
    public ExtendedToolStripSeparator()
    {
        this.Paint += ExtendedToolStripSeparator_Paint;
    }

    private void ExtendedToolStripSeparator_Paint(object sender, PaintEventArgs e)
    {
        // Get the separator's width and height.
        ToolStripSeparator toolStripSeparator = (ToolStripSeparator)sender;
        int width = toolStripSeparator.Width;
        int height = toolStripSeparator.Height;

        // Choose the colors for drawing.
        // I've used Color.White as the foreColor.
        Color foreColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardForeColorName);
        // Color.Teal as the backColor.
        Color backColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardBackColorName);

        // Fill the background.
        e.Graphics.FillRectangle(new SolidBrush(backColor), 0, 0, width, height);

        // Draw the line.
        e.Graphics.DrawLine(new Pen(foreColor), 4, height / 2, width - 4, height / 2);
    }
}

Then add the separator:

ToolStripSeparator toolStripSeparator = new ExtendedToolStripSeparator();

this.DropDownItems.Add(newGameToolStripMenuItem);
this.DropDownItems.Add(addPlayerToolStripMenuItem);
this.DropDownItems.Add(viewResultsToolStripMenuItem);
// Add the separator here.
this.DropDownItems.Add(toolStripSeparator);
this.DropDownItems.Add(exitToolStripMenuItem);

Result:

enter image description here

Szabolcs Antal
  • 877
  • 4
  • 15
  • 27
3

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

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • I saw this article but I'm looking for another solution, I do not think this is the best solution – Siwar Apr 10 '13 at 13:07
  • I also researched on it, but found no other suggestable alternative, this was the better alternative to practice it. – Freelancer Apr 10 '13 at 13:08
  • i put this code in the Menu.designer.cs this.fileToolStripMenuItem.BackColor = System.Drawing.SystemColors.ButtonFace; but it doesn't work – Siwar Apr 10 '13 at 13:09
  • This code worked for me, but I had to comment out the !e.Vertical test before it did. – Surfbutler Aug 29 '14 at 09:27
  • I added the code from the referenced source before the link breaks, otherwise this would be a 99% link-only answer anyway. – sɐunıɔןɐqɐp Apr 27 '21 at 11:56