WinForms中的多色线性渐变

发布于 2024-12-10 12:29:12 字数 82 浏览 0 评论 0原文

如何在WinForms中创建多色线性渐变? System.Drawing.Drawing2D.LinearGradientBrush 仅允许两种颜色。

How to create multi-color linear gradient in WinForms? System.Drawing.Drawing2D.LinearGradientBrush allows only two colors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画离情绘悲伤 2024-12-17 12:29:13

与这里的答案相同:Multi-color对角线渐变在winformsMulti-color对角线渐变在winforms

是一个小例子

void MainFormPaint(object sender, PaintEventArgs e)
{
  LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.Black, 0 , false);
  ColorBlend cb = new ColorBlend();
  cb.Positions = new[] {0, 1/6f, 2/6f, 3/6f, 4/6f, 5/6f, 1};
  cb.Colors = new[] {Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet};
  br.InterpolationColors= cb;
  // rotate
  br.RotateTransform(45);
  // paint
  e.Graphics.FillRectangle(br, this.ClientRectangle);
}

这里 结果

在此处输入图像描述

希望这有帮助

same answer as here: Multi-color diagonal gradient in winforms Multi-color diagonal gradient in winforms

Here is a little example

void MainFormPaint(object sender, PaintEventArgs e)
{
  LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.Black, 0 , false);
  ColorBlend cb = new ColorBlend();
  cb.Positions = new[] {0, 1/6f, 2/6f, 3/6f, 4/6f, 5/6f, 1};
  cb.Colors = new[] {Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet};
  br.InterpolationColors= cb;
  // rotate
  br.RotateTransform(45);
  // paint
  e.Graphics.FillRectangle(br, this.ClientRectangle);
}

here is the result

enter image description here

hope this helps

情魔剑神 2024-12-17 12:29:13

我创建了一个面板,因为我可以将其用作我的项目的容器。代码非常简单,就是如果你想实现 Punker76 提供的不停止。

public partial class GradientPanel : Panel
{
    private Color _startColor = Color.Transparent;
    private Color _stopColor = Color.Transparent;
    private bool _angleScales = false;
    private int _angle = 0;


    /// <summary>
    /// litle helper to redraw when property changes, a litle OnNofifyChanged for the 
    /// IDE causing it to update
    /// </summary>
    /// <typeparam name="T">Making sure the values are comparable</typeparam>
    /// <param name="field">the field that is compared</param>
    /// <param name="value">the value that is assigned</param>
    /// <remarks>Updates re-paints the panel when needed</remarks>
    private void Set<T>(ref T field, T value)
    {
        if (field.Equals(value))
            return;
        field = value;
        base.Refresh();
    }

    [Category("Appearence"),Description("Specifies the start colour")]
    public Color StartColor  {    get => _startColor;  set => Set(ref _startColor, value); }

    [Category("Appearence"), Description("Specifies the stop colour")]
    public Color StopColor { get => _stopColor; set => Set(ref _stopColor, value); }

    [Category("Appearence"), Description("The angle, measured in degrees clockwise from the x-axis, of the gradient's orientation line.")]
    public int Angle { get => _angle; set => Set(ref _angle , value);        }

    [Category("Appearence"), Description("If Set to true to specify that the angle is affected by the transform associated with this panel")]        
    public bool AngleScales { get => _angleScales; set => Set(ref _angleScales, value); }

    protected override void OnPaint(PaintEventArgs e)
    {

        LinearGradientBrush br = new LinearGradientBrush(e.ClipRectangle, StartColor, StopColor, Angle, AngleScales);
        e.Graphics.FillRectangle(br, e.ClipRectangle);
        base.OnPaint(e);
    }
}

拥有控件可以使重用更加高效。请记住,在控件中进行更改时,您可能需要重新构建项目才能看到更改生效。

I have created a panel as I can use this as container for my project. The code is really simple, that is if you want to implement no stops as provided by Punker76.

public partial class GradientPanel : Panel
{
    private Color _startColor = Color.Transparent;
    private Color _stopColor = Color.Transparent;
    private bool _angleScales = false;
    private int _angle = 0;


    /// <summary>
    /// litle helper to redraw when property changes, a litle OnNofifyChanged for the 
    /// IDE causing it to update
    /// </summary>
    /// <typeparam name="T">Making sure the values are comparable</typeparam>
    /// <param name="field">the field that is compared</param>
    /// <param name="value">the value that is assigned</param>
    /// <remarks>Updates re-paints the panel when needed</remarks>
    private void Set<T>(ref T field, T value)
    {
        if (field.Equals(value))
            return;
        field = value;
        base.Refresh();
    }

    [Category("Appearence"),Description("Specifies the start colour")]
    public Color StartColor  {    get => _startColor;  set => Set(ref _startColor, value); }

    [Category("Appearence"), Description("Specifies the stop colour")]
    public Color StopColor { get => _stopColor; set => Set(ref _stopColor, value); }

    [Category("Appearence"), Description("The angle, measured in degrees clockwise from the x-axis, of the gradient's orientation line.")]
    public int Angle { get => _angle; set => Set(ref _angle , value);        }

    [Category("Appearence"), Description("If Set to true to specify that the angle is affected by the transform associated with this panel")]        
    public bool AngleScales { get => _angleScales; set => Set(ref _angleScales, value); }

    protected override void OnPaint(PaintEventArgs e)
    {

        LinearGradientBrush br = new LinearGradientBrush(e.ClipRectangle, StartColor, StopColor, Angle, AngleScales);
        e.Graphics.FillRectangle(br, e.ClipRectangle);
        base.OnPaint(e);
    }
}

Having a control makes re-use so much more efficient. Remember, you may need to re-build your project before you see the changes take effect when you make changes in the control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文