Text Effects in Visual Basic 2008.. (Reflect)

In this tutorial I will teach you how to put an effect in the text using Visual Basic 2008. In here, I use the reflected text for the effect. It reflects any kind of text that you will type at the bottom and you can also change its font size. Others may think that reflected text is just easy to do. But, they were wrong. You have to take a closer look at each codes and calculate everything to make it accurate. Let’s begin: Open Visual Basic 2008, create a new Windows Application and drag a PictureBox, a TextBox, a ComboBox and a NumericUpDown. It will look like this. First Form Double click the Form, go to the solution explorer and click the view code. After that, create a sub procedure for the reflect effects.
  1. Private Sub DrawReflectText()
  2.         Dim text_size As SizeF
  3.         Dim grafx As Graphics
  4.         Dim back_brush As Brush = Brushes.Gray
  5.         Dim fore_brush As Brush = Brushes.Black
  6.         Dim Fnt As New Font("Microsoft Sans Serif", NumericUpDown1.Value, FontStyle.Regular)
  7.         Dim my_state As GraphicsState 'STORE THE CURRENT STATE OF GRAPHICS
  8.         Dim x_location, y_location As Single 'USED FOR THE LOCATION
  9.         Dim text_height As Single
  10.  
  11.         'CREATE A GRAPHIC OBJECT IN THE PICTURE BOX
  12.         grafx = PictureBox1.CreateGraphics()
  13.         'CLEAR THE GRAPHIC OBJECT
  14.         grafx.Clear(Color.White)
  15.  
  16.         'SIZE IS REQUIRED TO DRAW THE TEXT
  17.         text_size = grafx.MeasureString(TextBox1.Text, Fnt)
  18.  
  19.         'ELIMINATE THE REDUNDANT CALCULATIONS AFTER THE LOCATIONS' ONCE GET
  20.         x_location = (PictureBox1.Width - text_size.Width) / 2
  21.         y_location = (PictureBox1.Height - text_size.Height) / 2
  22.  
  23.         'IN SCALING THE ENTIRE GRAPHIC OBJECTS, WE NEED TO REPOSITION THE ORIGIN OF
  24.         'IT (0,0) TO THE (xLocation, yLocation) POINT.
  25.         'IF NOT, WHEN YOU FLIP THE TEXT WITH A SCALING TRANSFORM,
  26.         'IT WILL DRAW REFLECTED TEXT AT (xLocation, -yLocation)THAT IS OUTSITDE THE VIEWABLE AREA
  27.         grafx.TranslateTransform(x_location, y_location)
  28.  
  29.         Dim line_ascent As Integer
  30.         Dim line_spacing As Integer
  31.         Dim line_height As Single
  32.  
  33.         'USE GETCELLASCENT TO CALCULATE THE HIEGHT ABOVE THE BASELINE.
  34.         'SINCE IT RETURNS A DESIGN METRIC VALUE YOU HAVE TO CONVERT IT
  35.         'INTO PIXELS AND SCALED FOR THE FONT SIZE.
  36.         line_ascent = Fnt.FontFamily.GetCellAscent(Fnt.Style)
  37.         line_spacing = Fnt.FontFamily.GetLineSpacing(Fnt.Style)
  38.         line_height = Fnt.GetHeight(grafx)
  39.         text_height = line_height * line_ascent / line_spacing
  40.  
  41.         'THIS REFLECTS OVER THE LOWEST PORTION OF THE TEXT.
  42.         Dim line_descent As Integer 'REFLECT TO THE DESCENDING CHARACTERS
  43.         line_descent = Fnt.FontFamily.GetCellDescent(Fnt.Style)
  44.         text_height = line_height * (line_ascent + line_descent) / line_spacing
  45.  
  46.  
  47.         'DRAW THE REFLECTED ONE FIRST TO DEMONSTRATE THE USE OF GRAPHICS STATE OBJECT.
  48.         'A GRAPHICSSTATE OBJECT MAINTAINS THE GRAPHICS OBJECT.
  49.  
  50.         'GRAPHICSSTATE SAVE FIRST.
  51.         my_state = grafx.Save()S
  52.  
  53.         'USE THE SCALETRANSFORM WITH A NEGATIVE VALUE TO DRAW THE REFLECTIION
  54.         'AND USING -1 THE REFLECTED TEXT WILL NOT DISTORT.
  55.         grafx.ScaleTransform(1, -1.0F) ' REFLECTING THE Y DIRECTION
  56.         grafx.DrawString(TextBox1.Text, Fnt, back_brush, 0, -text_height)
  57.  
  58.         'BEFORE IT TRANFORM RESET THE GRAPHICS STATE
  59.         grafx.Restore(my_state)
  60.  
  61.         'MAIN TEXT DRAWN.
  62.         grafx.DrawString(TextBox1.Text, Fnt, fore_brush, 0, -text_height)
  63.  
  64.     End Sub
Then, create a sub procedure for the name of the effect. Clear and add the item in the ComboBox.
  1.     Private Sub effectlist()
  2.         With ComboBox1.Items
  3.             .Clear()
  4.             .Add("Reflect")
  5.         End With
  6.     End Sub
Then, create a sub procedure to draw the text.
  1.     Private Sub draw_text()
  2.         effectlist()
  3.         If ComboBox1.SelectedItem Is Nothing Then
  4.             ComboBox1.SelectedIndex = 0
  5.         End If
  6.         Select Case ComboBox1.SelectedItem.ToString()
  7.             Case "Reflect"
  8.                 Draw_Reflect_Text()
  9.         End Select
  10.     End Sub
Lastly, create a sub procedure for the UIchange.
  1.     Private Sub UIChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  2.         Handles TextBox1.TextChanged, NumericUpDown1.ValueChanged, PictureBox1.MouseHover
  3.         If NumericUpDown1.Value = 0 Then
  4.             NumericUpDown1.Value = 50
  5.         End If
  6.         draw_text()
  7.     End Sub
Ouput: Output

Add new comment