How to Apply Block Effects to a Text in VB.Net

0
392

This tutorial is all about How to Apply Block Effects to a Text in VB.Net.
Previously, I created a Tutorial about “How to Apply Reflect Effects to a Text in VB.Net“. Now, I will teach you how to “Block Effect” to a text using Visual Basic. With this, it will create the text with an embossed look. You can also adjust the Depth and even the font size of the text.

So let’s get started:

Open Visual Basic 2008 and open the file which is the “How to Apply Reflect Effects to a Text in VB.Net“. Then, add the Label and NumericDownUp named it “nudDepth” and set the value to 10 And it will look like this.

efecttext_form1bold

After that, go to the code viewer and create a sub procedure for the ” Block” effects.

[vbnet] Private Sub draw_block_text()
Dim text_size As SizeF
Dim grafx As Graphics
Dim back_brush As Brush = Brushes.Black ‘COLOR FOR THE BOARDER TEXT
Dim fore_brush As Brush = Brushes.RoyalBlue ‘COLOR FOR THE MAIN TEXT
Dim fnt As New Font(“Microsoft Sans Serif”, NumericUpDown1.Value, FontStyle.Regular)
Dim location_x, location_y As Single ‘USED IT FOR THE LOCATION
Dim i As Integer

‘CREATE A GRAPHIC OBJECT IN THE PICTUREBOX.
grafx = PictureBox1.CreateGraphics()
‘CLEAR THE PICTUREBOX
grafx.Clear(Color.White)

‘LOOK THE REQUIRED SIZE TO DRAW THE TEXT
text_size = grafx.MeasureString(Me.TextBox1.Text, fnt)

‘ELIMINATE THE REDUNDANT CAlCULATION AFTER GETTING THE LOCATION.
location_x = (PictureBox1.Width – text_size.Width) / 2
location_y = (PictureBox1.Height – text_size.Height) / 2

‘FIRST, DRAW THE BLACK BACKGROUND TO GET THE EFFECT,
‘AND THE TEXT MUST BE DRAWN REAPETEDLY FROM THE OFFSET RIGHT, UP TO THE MAIN TEXT IS DRAWN.
For i = CInt(nupDepth.Value) To 0 Step -1
grafx.DrawString(TextBox1.Text, fnt, back_brush, _
location_x – i, location_y + i)
Next

‘DRAW THE ROYAL BLUE FOR THE MAIN TEXT OVER THE BLACk TEXT
grafx.DrawString(TextBox1.Text, fnt, fore_brush, location_x, location_y)
End Sub
[/vbnet]

Then, in the “effectlist” sub procedure add the Block item in the Combobox. It will look like this.

[vbnet] Private Sub effectlist()
With ComboBox1.Items
.Clear()
.Add(“Reflect”)
.Add(“Block”)
End With
[/vbnet]

After that, update the code of the “draw_text” sub procedure and do this following code. The effects perform when the value of the ComboBox is changed.

[vbnet] Private Sub draw_text()
If ComboBox1.SelectedItem Is Nothing Then
effectlist()
ComboBox1.SelectedIndex = 0
End If

Select Case ComboBox1.SelectedItem.ToString()
Case “Reflect”
Draw_Reflect_Text()
nupDepth.Enabled = False
Case “Block”
draw_block_text()
nupDepth.Enabled = True
End Select
End Sub
[/vbnet]

Lastly, for the UIChange sub procedure add this “ComboBox1.SelectedValueChanged , nupDepth.ValueChanged” to the events handler. It will look like this.

[vbnet] Private Sub UIChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged, NumericUpDown1.ValueChanged, PictureBox1.MouseHover, _
ComboBox1.SelectedValueChanged, nupDepth.ValueChanged

If NumericUpDown1.Value = 0 Then
NumericUpDown1.Value = 50
End If
draw_text()

End Sub
[/vbnet]

Output:

efecttext_outputbold

 

Readers might read also:

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.