2012-03-20 3 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에 추가하고 브러시로 경로를 그려보십시오. –

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

} 

}