Animation: Animated Text in Visual Basic 2008

Today, I will teach you how to animate a text by using Visual Basic 2008. With this, you don’t need to drag any Label in the Form. It is because, the codes will be the one to make a text that will be appear in the Form. So let’s begin: Open Visual Basic 2008, create a new Windows Application. And drag a Timer in the Form. first form Note: Put this code for the imports
  1. Imports System.Drawing.Drawing2D
  2. Imports System.Drawing.Text
After that, double click the Timer and do the following code above the Timer_Tick sub procedure for declaring the variables that are needed.
  1. Const timer_interval As Integer = 15 ' INTERVAL IN MILLISECONDS
  2. Protected current_gradient_shift As Integer = 10
  3. Protected gradiant_step As Integer = 5
In the Timer_Tick sub procedure, do the following code for the animation.
  1. Private Sub Timer_Tick(ByVal obj As Object, ByVal ea As EventArgs) Handles Timer1.Tick
  2. 'SET THE GRAPHICS OBJECT IN THE FORM
  3. Dim grafx As Graphics = CreateGraphics()
  4.  
  5. 'SET AND DETERMINE THE SIZE,FONT AND TEXT.
  6. Dim fnt As New Font("Segoe UI", 40, _
  7. FontStyle.Regular, GraphicsUnit.Point)
  8. Dim start_text As String = "Text Animation" 'APPEAR THE TEXT IN THE FIRST LOAD
  9. Dim fnt_size As New SizeF(grafx.MeasureString(start_text, fnt))
  10.  
  11. 'SET THE TEXT THAT TO BE CENTERED IN THE CLIENT AREA.
  12. Dim ptf_text_start As New PointF( _
  13. CSng(ClientSize.Width - fnt_size.Width) / 2, _
  14. CSng(ClientSize.Height - fnt_size.Height) / 2)
  15.  
  16.  
  17. 'FOR THE ANIMATION EFFECT, SET THE GRADIENT START AND ITS END POINT.
  18. Dim ptf_gradient_start As New PointF(0, 0)
  19. Dim ptf_gradient_end As New PointF(current_gradient_shift, 130)
  20.  
  21.  
  22. 'USE THE BRUSH FOR DRAWING THE TEXT.
  23. Dim gradient_brush As New LinearGradientBrush(ptf_gradient_start, _
  24. ptf_gradient_end, Color.Crimson, BackColor)
  25.  
  26. 'THE TEXT DRAW AT THE CENTERED OF THE CLIENT AREA.
  27. grafx.DrawString(start_text, fnt, gradient_brush, ptf_text_start)
  28. grafx.Dispose()
  29.  
  30. 'REVERSING THE GRADIENT WHEN IT GETS TO A CERTAIN VALUE
  31. current_gradient_shift += gradiant_step
  32. If current_gradient_shift = 500 Then
  33. gradiant_step = -5
  34. ElseIf current_gradient_shift = -50 Then
  35. gradiant_step = 5
  36. End If
  37. End Sub
Then, go back to the design views, double click the Form and do the following code for starting the Time and to start the animation on the first load.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Timer1.Start()
  3. Timer1.Interval = timer_interval
  4. End Sub
Output : output

Add new comment