标题如何实现文本动态显示
栏目软件世界
作者李志龙
发布2001年38期
我们编写VB程序时,有时需在启动窗体上显示一些文本内容,作些细节说明,如果能在屏幕上产生逐字打印特效,使文本内容动态显示,则画面将会更生动有趣。下面就是一个特效文本显示程序。
第一步:新建一个窗体,设置窗体属性。设置好窗体大小、窗体背景色、字体、字体颜色。
第二步:在窗体上添加两个计时器。设置Timer1的属性:Interval=200,设置Timer2的属性:Enabled=False,interval=5000。
第三步:编写代码。
Dim l As Integer
Dim i As Integer
Dim x As Integer
Dim y As Integer
Dim page As Integer
Dim filenum As Integer
Dim words As String
Private Sub Form_Load()
ChDrive App.Path
ChDir App.Path
FileName$ = "test.txt"
filenum = FreeFile
Open FileName$ For Input As #filenum
Do While Not EOF(filenum)
Line Input #filenum, Text$
words = words + Text$ + Chr(13) + Chr(10) '将文本内容赋值给words串变量,包括回车符和换行符
Loop
Close #filenum
l = Len(words) '计算words串变量长度(字符个数)
End Sub
Private Sub Timer1_Timer()
If page = 1 Then '满一页后清除窗体内容
Form1.Cls
page = 0
Timer1.Interval = 200 '重置打字速度
End If
typewrite '调用打字子例程
End Sub
Public Sub typewrite()
i = i + 1 '字符计数器
x = x + 1
CurrentX = 100 '设置打字的起始点
CurrentY = 300
CurrentX = CurrentX + x * 250 '设置字间距
CurrentY = CurrentY + y * 280 '设置行间距
t$ = Mid$(words, i, 1) '从words串中指定位置取一个汉字或标点符号
Print t$ '在窗体上打印一个字符
If x = 20 Or Mid$(words, i, 2) = Chr$(13) + Chr$(10) Then '设置行字符个数,若行字符数满或段落结束(遇回车换行符)则换行
y = y + 1
x = 0
End If
If y = 12 Then '设置行数
page = 1 '行数满为一页
y = 0
Timer1.Interval = 6000 '设置页显示停留时间
End If
If i > l Then '文本内容打印完毕,计时器2打开
Timer1.Enabled = False
Timer2.Enabled = True
End If
End Sub
Private Sub Timer2_Timer()
Timer2.Enabled = False
Timer1.Enabled = False
End '改变end,可执行其他子例程
End Sub
最后,编辑好预显示的文本内容,即可运行文本动态显示程序。