2012-03-20 24 views
0

ラベルテキストにグラデーションを適用できますか?グラデーションラベルを描画

今、私はコントロールのOnPaintを引き継いで、必要なテキストの文字列を描画しています。しかし、これは具体的なものです。私は本当にラベル自体が私が欲しいグラデーションの色が適用されるようにしたい。したがって、各文字は、テキストが変更されたときに指定された勾配を持ちます。

ForeColorを使用する代わりに、私はLinearGradientBrushを適用します。私は現時点でWinFormsを使用しています。ここで

EDIT 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にテキストを追加し、あなたのブラシでパスを描画してみてください。 –

+0

フレームワークのバージョン4を使用している場合は、 'FormattedText'オブジェクトが役に立ちます:http://msdn.microsoft.com/en-us/library/ms752098.aspx – SeeSharp

+0

@SeeSharpこれは非常にそうですWPF固有のものです。 WinFormsにはOnRenderメソッドがありません。 – meanbunny

答えて

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

} 

}