|
|
Title | Fill text with a moving color gradient in Visual Basic .NET |
Description | This example shows how to fill text with a moving color gradient in Visual Basic .NET. |
Keywords | text, color gradient, moving gradient, animation, VB.NET |
Categories | Graphics, VB.NET, Multimedia |
|
|
When the form's Timer fires, the event handler invalidates the drawing area PictureBox.
The PictureBox's Paint event handler makes a LinearGradientBrush. It defines a color blend to make the brush shade from red to green to blue. It sets the positions of the colors so red and blue are at the ends and green is positioned according to the variable m_Middle.
The program makes a StringFormat object to defined centered text. It then makes a GraphicsPath object, adds some text to it, and fills the text with the gradient brush.
It then updates m_Middle so the green part will move next time. If m_Middle moves beyond 0 or 1, the program changes the sign of m_Delta, the amount by which it adjusts m_Middle, so the movement goes in the other direction next time.
|
|
Private m_Middle As Single = 0
Private m_Delta As Single = 0.1
Private Sub tmrRotate_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tmrRotate.Tick
picCanvas.Invalidate()
End Sub
Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles _
picCanvas.Paint
' Make the gradient brush.
Dim br As New LinearGradientBrush(New Point(0, 0), New _
Point(Me.ClientSize.Width, 0), Color.Red, _
Color.Blue)
Dim color_blend As New ColorBlend
color_blend.Colors = New Color() {Color.Red, _
Color.Lime, Color.Blue}
color_blend.Positions = New Single() {0, m_Middle, 1}
br.InterpolationColors = color_blend
' Fill some text with it.
Dim string_format As New StringFormat
string_format.Alignment = StringAlignment.Center
string_format.LineAlignment = StringAlignment.Center
Dim graphics_path As New GraphicsPath
graphics_path.AddString("Gradient Text", _
Me.Font.FontFamily, _
CInt(FontStyle.Bold), Me.Font.Size, _
picCanvas.ClientRectangle, _
string_format)
e.Graphics.FillPath(br, graphics_path)
br.Dispose()
' Adjust the gradient's midpoint.
m_Middle += m_Delta
If (m_Middle > 1) OrElse (m_Middle < 0) Then m_Delta = _
-m_Delta
End Sub
|
|
|
|
|
|