2

我正在做一个应用程序,我有 WPF 表单和 winforms。我在 WPF 中制作了一个很酷的背景渐变,但我也需要在我的 Windows 窗体中使用它。我尝试使用此代码,但它不起作用。它以 Windows 形式绘制渐变,但与 WPF 不同。

这是 WPF 的代码:

<Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFFCFEFF" Offset="0" />
            <GradientStop Color="#FFA6BAD0" Offset="1" />
            <GradientStop Color="#FFE4EFF7" Offset="0.317" />
            <GradientStop Color="#FFC8D4E3" Offset="0.585" />
            <GradientStop Color="#FFB1C6D7" Offset="0.797" />
            <GradientStop Color="#FFF7FBFD" Offset="0.146" />
            <GradientStop Color="#FFD9E4EE" Offset="0.439" />
        </LinearGradientBrush>
    </Grid.Background>

这里是我的 Winform 代码(颜色相同):

public void Form_Background(object sender, PaintEventArgs e)
    {
        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, 200, 212, 217);
        Color c6 = Color.FromArgb(255, 177, 198, 215);
        Color c7 = Color.FromArgb(255, 166, 186, 208);

        LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.Black, 0, false);
        ColorBlend cb = new ColorBlend();
        cb.Positions = new[] { 0, (float)0.146, (float)0.317, (float)0.439, (float)0.585, (float)0.797 , 1};
        cb.Colors = new[] { c1, c2, c3, c4, c5, c6, c7 };
        br.InterpolationColors = cb;

        // rotate
        br.RotateTransform(90);

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

希望你们能帮助我,我需要快速解决这个问题,我不想使用图像背景或在我的 winforms 中放置 WPF 控件..

谢谢你的帮助!

4

1 回答 1

1

我不确定这是否会完全正确,因为您没有包含不同之处的屏幕截图,但我会尝试一下:

public void Form_Background(object sender, PaintEventArgs e)
    {
        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, 200, 212, 217);
        Color c6 = Color.FromArgb(255, 177, 198, 215);
        Color c7 = Color.FromArgb(255, 166, 186, 208);

        // Changed: c1 / c7 as start colors, and at 90 degrees.  Removed later transform.
        LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, c1, c7, 90, true);
        ColorBlend cb = new ColorBlend();
        cb.Positions = new[] { 0, (float)0.146, (float)0.317, (float)0.439, (float)0.585, (float)0.797, 1 };
        cb.Colors = new[] { c1, c2, c3, c4, c5, c6, c7 };
        br.InterpolationColors = cb;

        // removed rotate call

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

这看起来是正确的,将 XAML 设计器与我的 WinForms 输出(左侧 XAML 设计器,右侧运行 WinForms 应用程序)进行比较: 渐变

于 2012-12-05T01:48:22.513 回答