Search from Cycling Blog

Draw a background with a rotating color gradient in Visual Basic .NET

TitleDraw a background with a rotating color gradient in Visual Basic .NET
DescriptionThis example shows how to draw a background with a rotating color gradient in Visual Basic .NET.
Keywordscolor gradient, moving gradient, rotating gradient, animation, VB.NET



When the form's Timer fires, the event handler invalidates the drawing area PictureBox.

The PictureBox's Paint event handler makes a LinearGradientBrush. The rectangle the gradient covers is slightly larger than the PictureBox's client rectangle. The brush uses the angle m_Theta to determine the gradient's angle.

The program fills the PictureBox with the brush. It then updates m_Theta so the gradient rotates next time.

Finally the program draws some text over the colored background.


Private m_Theta As Single = 0
Private m_Delta As Single = 10

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
Dim rect As New Rectangle(-3, -3, _
picCanvas.ClientSize.Width + 6, _
picCanvas.ClientSize.Height + 6)
Dim br As New LinearGradientBrush(rect, Color.Red, _
Color.Blue, m_Theta)
e.Graphics.Clear(picCanvas.BackColor)
e.Graphics.FillRectangle(br, Me.ClientRectangle)
br.Dispose()
m_Theta += m_Delta

Dim string_format As New StringFormat
string_format.Alignment = StringAlignment.Center
string_format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString("Some Text", Me.Font, _
Brushes.Black, _
picCanvas.ClientSize.Width \ 2, _
picCanvas.ClientSize.Height \ 2, _
string_format)
End Sub
Download Source Code here

 

0 comments: