2012-03-20 62 views
0

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

现在我接管一个控件OnPaint并绘制我想要的文本字符串;但是,这是具体的。我真的很想做到这一点,使标签本身得到应用我想要的渐变颜色。因此,每个字符都会在文本发生变化时指定渐变。

因此,不使用ForeColor,我会应用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); 

    } 
} 

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

谢谢!

+1

关闭我的头顶,尝试将文本添加到GraphicsPath并使用画笔绘制路径。 – 2012-03-20 15:43:50

+0

如果您使用的是框架的第4版,那么'FormattedText'对象可能对您有所帮助:http://msdn.microsoft.com/en-us/library/ms752098.aspx – SeeSharp 2012-03-20 15:44:23

+0

@SeeSharp这似乎是非常非常特定于WPF。我在WinForms中没有OnRender方法。 – meanbunny 2012-03-20 15:49:46

回答

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

} 

}