面板背景油漆不起作用

时间:2012-12-22 17:52:49

标签: c# winforms gradient paint

我一直在用面板做winforms的边框。我想在我的底部面板中使用渐变,但我遇到了问题。

我使用的方法与我在此处绘制表单背景的方法几乎相同,但是我在面板中得到了一个红十字。我不知道为什么会这样,它应该可以正常工作。

图片:enter image description here

这是我的代码:

public void Colorear_Barra_abajo(object sender, PaintEventArgs e)
    {
        Rectangle r = this.ClientRectangle;

        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.FromArgb(255, 54, 54, 54);
            Color c2 = Color.FromArgb(255, 98, 98, 98);

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c2, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { (float)0.357, (float)0.914 };
            cb.Colors = new[] { c1, c2 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }

这就是我所说的(panel_Borde_abajo是面板):

public Base_Form_Standard()
    {
        InitializeComponent();
        panel_Borde_abajo.Paint += new PaintEventHandler(Colorear_Barra_abajo);
    }

我曾经使用这种方法来绘制除形式之外的其他控件(menustrip f.e),并且它们工作得很好,但是这个特别的不起作用。

例如,它可以正常工作,不会出现红叉:

public void Form_Background(object sender, PaintEventArgs e)
    {
        Rectangle r = this.ClientRectangle;

        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.FromArgb(255, 252, 254, 255);
            Color c2 = Color.FromArgb(255, 247, 251, 253);
            Color c3 = Color.FromArgb(255, 228, 239, 247);
            Color c4 = Color.FromArgb(255, 217, 228, 238);
            Color c5 = Color.FromArgb(255, 177, 198, 215);

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c5, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.3, (float)0.486, (float)0.786, 1 };
            cb.Colors = new[] { c1, c2, c3, c4, c5 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }

如果有人能帮助我,那就太棒了!

1 个答案:

答案 0 :(得分:3)

看起来,您已为 ColorBlend.Positions 属性使用了无效值,请尝试以下代码:

ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0.0f, 0.357f, 0.914f, 1.0f };
                       ^^^^                  ^^^^

根据documentation

  

此数组中的元素由浮点值表示   在0.0f和1.0f之间,数组的第一个元素必须是0.0f   最后一个元素必须是1.0f。