(1)新建一个名为“DrawFont”的项目,然后在窗体设计器中,添加4个Button按钮,对窗体和各个控件设置其属性如下:
Form1的Text属性:文字特效 BackGroupImage属性:C:\windows\Web\Wallpaper\Bliss.jpg
Button1的name属性:btnSD Text属性:3D文字
Button1的name属性:btnYY Text属性:阴影文字
Button1的name属性:btnJB Text属性:渐变文字
Button1的name属性:btnTA Text属性:图案文字
运行效果图如下:
(2)为控件添加相应的代码,完整代码如下:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Private flag As Integer
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim fntText = New Font("隶书", 40)
Dim g As Graphics = e.Graphics
Dim brB, brF As Brush
Dim bm As Image = Image.FromFile("D:\WINDOWS\Web\Wallpaper\20.jpg")
Dim i As Integer
Dim p1, p2 As Point
p1.X = 20
p1.Y = 20
p2.X = 200
p2.Y = 20
Select Case flag
Case 1
brB = New SolidBrush(Color.FromArgb(100, Color.Black))
brF = New SolidBrush(Color.Red)
For i = 1 To 10
g.DrawString("Visual Basic.Net", fntText, brB, p1.X + i, p1.Y + i)
g.DrawString("程序设计", fntText, brB, p1.X + 150 + i, p1.Y + 50 + i)
Next
g.DrawString("Visual Basic.Net", fntText, brB, p1.X, p1.Y)
g.DrawString("程序设计", fntText, brB, p1.X + 150, p1.Y + 50)
Case 2
brB = New SolidBrush(Color.FromArgb(120, Color.Black))
brF = New SolidBrush(Color.Red)
g.DrawString("Visual Basic.Net", fntText, brB, p1.X + 5, p1.Y + 5)
g.DrawString("程序设计", fntText, brB, p1.X + 150 + 5, p1.Y + 50 + 5)
g.DrawString("Visual Basic.Net", fntText, brB, p1.X, p1.Y)
g.DrawString("程序设计", fntText, brB, p1.X + 150, p1.Y + 50)
Case 3
brF = New LinearGradientBrush(p1, p2, Color.Red, Color.Yellow)
g.DrawString("Visual Basic.Net", fntText, brF, p1.X, p1.Y)
g.DrawString("程序设计", fntText, brF, p1.X + 150, p1.Y + 50)
Case 4
brF = New TextureBrush(bm)
g.DrawString("Visual Basic.Net", fntText, brF, p1.X, p1.Y)
g.DrawString("程序设计", fntText, brF, p1.X + 150, p1.Y + 50)
End Select
End Sub
Private Sub btnSD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSD.Click
flag = 1
Me.Invalidate()
End Sub
Private Sub btnYY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYY.Click
flag = 2
Me.Invalidate()
End Sub
Private Sub btnJB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJB.Click
flag = 3
Me.Invalidate()
End Sub
Private Sub btnTA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTA.Click
flag = 4
Me.Invalidate()
End Sub
End Class
(3)程序说明:
程序开头要用到两个命名空间,所以加上两行代码:
Imports System.Drawing
Imports System.Drawing.Drawing2D
其次为窗体添加Paint事件,文字的3D,阴影,渐变,图案效果都是在窗体的Paint事件中实现的,所谓窗体的Paint事件,说的简单点就是一个画图事件。