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

0
478

This tutorial is all about How to Apply Shear Effects to a Text in VB.Net.
This is the continuation of my last topic which is How to Apply “Block Effects” to a Text in VB.Net. This time, I’m going to add another text effect which is the ”Shear”. With the use of “Shear”, you can slide the text from left to right or right to left. At the same time, you can adjust the slide effects of the text whatever sliding position you like.

Let’s begin:

First, open the file of “How to Apply “Shear Effects” to a Text in VB.Net”. After that, add new “NumericUpDown” and name it “nudShear”. It will look like this.

efecttext_form1shear

Then, go to the Solution Explorer and click the code view. After that, do the following code in creating a sub procedure for the “Shear” effects.

[vbnet] Private Sub draw_shear_text()
Dim text_size As SizeF
Dim grafx As Graphics
Dim fore_brush As Brush = Brushes.Blue
Dim fnt As New Font(“Microsoft Sans Serif”, NumericUpDown1.Value, FontStyle.Regular)
Dim tranform As Matrix
Dim location_x, location_y As Single

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

‘SET THE REQUIRE SIZE TO DRAW THE TEXT.
text_Size = grafx.MeasureString(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

‘REPOSITION THE ORIGIN OF THE GRAPHICS OBJECT (0,0) TO THE (location_x,
‘ location_y) POINT.
grafx.TranslateTransform(location_x, location_y)

‘SPECIFY THE AMOUNT TO TRANSFORM THE CURRENT GRAPHICS OBJECT TO A SHEAR TEXT.
tranform = grafx.Transform
tranform.Shear(nudShear.Value, 0)
grafx.Transform = tranform

‘MAIN TEXT DRAWN.
grafx.DrawString(TextBox1.Text, fnt, fore_brush, 0, 0)
End Sub
[/vbnet]

Update the “effectlist” sub procedure. Add the item in the ComboBox which is “Shear”.

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

Update the “draw_text” sub procedure. Add another condition which is “Shear”. The function of this condition is when the value of a ComboBox is changed, the effect of the text will be changed too.

[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
nudShear.Enabled = False
Case “Block”

draw_block_text()
nupDepth.Enabled = True
nudShear.Enabled = False
Case “Shear”

draw_shear_text()
nupDepth.Enabled = False
nudShear.Enabled = True

End Select
End Sub
[/vbnet]

Add the “ nudShear.ValueChanged ” for the events of the “UIChanged” sub procedure. With this, when the value of “nudShear” is changed, the slide of the text will be changed too. And 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, nudShear.ValueChanged

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

End Sub
[/vbnet]

Output:

efecttext_outputshear

Download the complete sourcecode here

 

Readers might read also:

How to Create a Bouncing Ball in VB.Net

How to Create a Simple Tic Tack Toe Game using VB.Net

 

 

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.