C# 绘制渐变标签

C# 绘制渐变标签,c#,winforms,custom-controls,paint,C#,Winforms,Custom Controls,Paint,是否可以对标签文本应用渐变 现在,我正在接管绘画控件,并绘制我想要的文本字符串;然而,这是非常具体的。我真的想让它,使标签本身得到应用梯度颜色我想要的。因此,当文本发生变化时,每个字符都会有指定的渐变 因此,我不使用前景色,而是使用LinearGradientBrush。我现在正在使用WinForms 编辑1 这是我目前正在使用的代码。但是,这仅将渐变应用于所有字符。我想更改它,以便应用字符串中的每个字符 // Draw the formatted text string to the Draw

是否可以对标签文本应用渐变

现在,我正在接管绘画控件,并绘制我想要的文本字符串;然而,这是非常具体的。我真的想让它,使标签本身得到应用梯度颜色我想要的。因此,当文本发生变化时,每个字符都会有指定的渐变

因此,我不使用前景色,而是使用LinearGradientBrush。我现在正在使用WinForms

编辑1

这是我目前正在使用的代码。但是,这仅将渐变应用于所有字符。我想更改它,以便应用字符串中的每个字符

// Draw the formatted text string to the DrawingContext of the control.
Font font = new Font("BankGothic Md BT", 48f, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(label1.Location, new Point(label1.Width, label1.Height), Color.Goldenrod, Color.Black);
e.Graphics.DrawString(label1.Text, font, brush, 0,0);
编辑2


这就是我所做的。我只是扩展了Label类并继承了OnPaint

public partial class LabelEx : Label {
    public LabelEx() {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e) {
        // Draw the formatted text string to the DrawingContext of the control.
        //base.OnPaint(e);
        Font font = new Font("Tahoma", 48f, FontStyle.Bold);
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.Gold, Color.Black, LinearGradientMode.Vertical);
        e.Graphics.DrawString(Text, font, brush, 0, 0);

    }
}
这给了我一个很好的渐变文本标签


谢谢

以下是我所做的。我只是扩展了Label类并继承了OnPaint

public partial class LabelEx : Label {

public LabelEx() {
    InitializeComponent();
}

protected override void OnPaint(PaintEventArgs e) {
    // Draw the formatted text string to the DrawingContext of the control.
    //base.OnPaint(e);
    Font font = new Font("Tahoma", 48f, FontStyle.Bold);
    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.Gold, Color.Black, LinearGradientMode.Vertical);
    e.Graphics.DrawString(Text, font, brush, 0, 0);

}

}

在我的脑海中,尝试将文本添加到图形SPAH中,并用画笔绘制路径。如果您使用的是框架的第4版,
FormattedText
对象可能会对您有所帮助:@SeeSharp这似乎对WPF非常具体。我在WinForms中没有OnRender方法。@meanbunny啊,你是对的-对不起,我读得太快了,没有听到你说你在使用WinForms。很抱歉。请以答案的形式发布,而不是对您的问题进行编辑。