Sunday, 18 December 2016

Animated Text in Visual Basic 2010

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 2010, create a new Windows Application. And drag a Timer in the Form.

Note: Put this code for the imports
                      Imports System.Drawing.Drawing2D
                      Imports System.Drawing.Text
After that, double click the Timer and do the following code above the Timer_Ticksub procedure for declaring the variables that are needed.
                    Const timer_interval As Integer = 15 ' INTERVAL IN MILLISECONDS
                    Protected current_gradient_shift As Integer = 10
                    Protected gradiant_step As Integer = 5
In the Timer_Tick sub procedure, do the following code for the animation.
Private Sub Timer_Tick(ByVal obj As Object, ByVal ea As EventArgs) Handles Timer1.Tick
        'SET THE GRAPHICS OBJECT IN THE FORM
        Dim grafx As Graphics = CreateGraphics()

        'SET AND DETERMINE THE SIZE,FONT AND TEXT.
        Dim fnt As New Font("Segoe UI", 60, _
            FontStyle.Regular, GraphicsUnit.World)
        Dim start_text As String = "Text Animation" 'APPEAR THE TEXT IN THE FIRST LOAD
        Dim fnt_size As New SizeF(grafx.MeasureString(start_text, fnt))

        'SET THE TEXT THAT TO BE CENTERED IN THE CLIENT AREA.
        Dim ptf_text_start As New PointF( _
            CSng(ClientSize.Width - fnt_size.Width) / 2, _
           CSng(ClientSize.Height - fnt_size.Height) / 2)


        'FOR THE ANIMATION EFFECT, SET THE GRADIENT START AND ITS END POINT.
        Dim ptf_gradient_start As New PointF(0, 0)
        Dim ptf_gradient_end As New PointF(current_gradient_shift, 130)


        'USE THE BRUSH FOR DRAWING THE TEXT.
        Dim gradient_brush As New LinearGradientBrush(ptf_gradient_start, _
            ptf_gradient_end, Color.Chocolate, BackColor)

        'THE TEXT DRAW AT THE CENTERED OF THE CLIENT AREA.
        grafx.DrawString(start_text, fnt, gradient_brush, ptf_text_start)
        grafx.Dispose()

        'REVERSING THE GRADIENT WHEN IT GETS TO A CERTAIN VALUE
        current_gradient_shift += gradiant_step
        If current_gradient_shift = 500 Then
            gradiant_step = -5
        ElseIf current_gradient_shift = -50 Then
            gradiant_step = 5
        End If

    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.
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
        Timer1.Interval = timer_interval
    End Sub
A Code that will be appear in the following:

Output:




0 comments:

Post a Comment