绘制渐变标签

问题描述 投票:0回答:1

是否可以将渐变应用于标签文本?

现在,我要接管OnPaint控件并绘制所需的文本字符串;但是,这是特定的。我真的很想这样做,以便标签本身可以应用我想要的渐变颜色。因此,随着文本的更改,每个字符都将具有指定的渐变。

因此,我将使用LinearGradientBrush而不是使用ForeColor。我目前正在使用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);

    }
}

这给了我一个不错的渐变文本标签。

谢谢!

c# winforms custom-controls paint
1个回答
1
投票

这是我所做的。我只是扩展了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);

}

}

0
投票

此标签未使用文本对齐方式。请添加。

© www.soinside.com 2019 - 2025. All rights reserved.