2012-03-20 107 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); 

} 

}