-->

更改ToolStripSeparator控制的背景色(Change the BackColor of

2019-08-21 07:49发布

是否有可能改变ToolStripSeparator控制的背景色? 有在设计一个背景色属性,但它似乎并没有被使用 - 颜色始终为白色。

Answer 1:

默认的toolstrip渲染忽略的背景色属性,并使用硬编码的颜色。

你可以参考下面的链接继续用自己的渲染绘制分隔你想要的方式。

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



Answer 2:

我看到这个问题2年前有人问,但我还是不能找到这个在网络上简单明了的解决方案。 所以...

我刚刚所面临的问题,今天发现,这是很简单的解决这个问题。

具有同样的情况:

解:

创建继承了一类ToolStripSeparator类,并添加一个方法到Paint EventHandler绘制分隔符:

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);
    }
}

然后添加分隔符:

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);

结果:



Answer 3:

我只是指出我的分隔符Paint事件这一习俗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);

    }

凡CUSTOM_COLOR_FOREGROUND是固体/名为Color,如Color.White例如。



Answer 4:

http://www.c-sharpcorner.com/uploadfile/mahesh/toolstrip-in-C-Sharp/

请参考上面的链接。 我希望可以帮助你!



文章来源: Change the BackColor of the ToolStripSeparator control
标签: c# toolstrip