3

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

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

图片:在此处输入图像描述

这是我要绘制的代码:

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 fe)以外的其他控件并且它们工作得很好,但是这个特别的不工作。

例如,这确实可以正常工作,并且没有出现红十字:

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

如果有人可以帮助我,那就太好了!

4

1 回答 1

3

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

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

根据文档

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

于 2012-12-22T18:03:19.623 回答