如何将ToolStripContainer的BackColor设置为渐变颜色

时间:2014-06-03 12:44:22

标签: c# .net winforms visual-studio

我在winform上有一个ToolStripContainer。我想将其ContentPanel的BackColor设置为自定义渐变颜色。

我该如何设置?

2 个答案:

答案 0 :(得分:2)

您可以创建一个继承自ToolStripContainer的简单类,并在其构造函数中设置BackgroundImage(或其他任何内容)。例如:

class MyToolStripContainer : System.Windows.Forms.ToolStripContainer
{
    public MyToolStripContainer()
    {
        var rect = new Rectangle(0, 0, 300, 300);
        using (Bitmap bitmap = new Bitmap(rect.Height, rect.Width))
        using (Graphics graphics = Graphics.FromImage(bitmap))
        using (LinearGradientBrush brush = new LinearGradientBrush(
            rect,
            Color.Blue,
            Color.Red,
            LinearGradientMode.ForwardDiagonal))
        {
            brush.SetSigmaBellShape(0.5f);
            graphics.FillRectangle(brush, rect);
            ContentPanel.BackgroundImage = Image.FromHbitmap(bitmap.GetHbitmap());
        }
        ContentPanel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;

    }
}

答案 1 :(得分:0)

不仅ToolStripContentPanel,没有winform控件支持"Gradient colors"开箱即用,而且它们提供了自定义绘画的方法。这就是我们如何在look and feel中完成渐变winforms

toolStripContainer1.ContentPanel.Paint += ContentPanel_Paint;//Triggering up the paint event

public void ContentPanel_Paint(object sender, PaintEventArgs e)
{
    ToolStripContentPanel panel = (ToolStripContentPanel)sender;
    using (var brush = new LinearGradientBrush(panel.ClientRectangle, Color.Gray, Color.Red, 90))
    {
        e.Graphics.FillRectangle(brush, panel.ClientRectangle);
    }
}