跟我一起玩个性──用VB制作动画屏保

在前面,我们学习了如何在Windows 98中实现Windows XP的某些个性化功能和一些有关系统、软件的个性化设置。那么,对于我们编程爱好者而言,能够自己动手制作才算“真英雄”。OK,就请你跟着来一起制作动画屏保吧,相信你一定能从中学到不少好东西!
   我们知道,Windows系统中的屏幕保护程序扩展名为.scr,千万别以为这是微软的什么秘密文件格式,它只不过是一种在系统控制下的可执行文件(.exe文件)被换名存放在C盘Windows目录下罢了。因此,用VB的动画设计方法也可以做出同样精彩的屏保来。现在,就让我们来一起动手,在你的屏幕上做个“星空”吧!本例的重点在于学习在VB中如何控制图片。
   启动VB 6.0后,新建一个工程,在窗体上放置三个计时器,timer1的interval值设为90,timer2 和timer3的interval值均设为500,再设置两个图片组:Picture2到picture9为一组,Picture0和picture1为一组。其中,除picture1和picture2的可视化属性(visible)设为TRUE外,其它图片的visible属性一律设为FALSE。
   好了,现在双击窗体,写入以下代码:
   Private Declare Function ShowCursor Lib“user32”(ByVal bShow As Long)As Long
   Private Declare Function SystemParametersInfo Lib “user32” Alias “systemparametersinfoa” () As Long
   Dim z As Integer
   Private Sub form_load()
   temp = ShowCursor(0)
   '隐藏鼠标
   DrawWidth = 1
   '在背景上画小点
   Picture1.Left = 0: Picture1.Top = 0.3 * form1.Width
   Picture1.Width = 600: Picture1.Height = 800
   z = 0
   End Sub
   Private Sub timer2_timer()
   Select Case z
   Case 0
   Picture2.Picture = Picture3.Picture: z = 1
   Case 1
   Picture2.Picture = Picture2.Picture: z = 2
   Case 2
   Picture2.Picture = Picture5.Picture: z = 3
   Case 3
   Picture2.Picture = Picture6.Picture: z = 4
   Case 4
   Picture2.Picture = Picture8.Picture: z = 5
   Case 5
   Picture2.Picture = Picture9.Picture: z = 6
   z = 0
   End Select
   '交替显示图片,形成动画效果
   For i = 0 To 100
   i = i + 1
   Next
   Picture2.Left = Picture2.Left + i
   '通过不断增加图片的左边距来达到移动图片的目的
   If Picture2.Left >= form1.Width Then
   Picture2.Left = 0
   Picture2.Top = Rnd * form1.Height
   form1.Cls
   form1.Show
   '设置图片移出界面后的处理方法:图片回到左边界,清屏后再显示背景
   End If
   End Sub
   Private Sub timer3_timer()
   Select Case z
   Case 0
   Picture1.Picture = Picture7.Picture: z = 1
   Case 1
   Picture1.Picture = Picture0.Picture: z = 2
   Case 2
   Picture1.Picture = Picture7.Picture: z = 3
   z = 0
   End Select
   For i = 1 To 90
   i = i + 1
   Next
   Picture1.Left = Picture1.Left + i
   Picture1.Top = Picture1.Top + 0.5 * i
   If Picture1.Left = 0 Or Picture1.Left >= form1.Width - Picture1.Width Then
   Picture1.Left = 0
   form1.Cls
   form1.Show
   End If
   If Picture1.Top = 0 Or Picture1.Top>= form1.Height Then
   form1.Cls
   form1.Show
   Picture1.Top = Rnd * form1.Height + i
   End If
   End Sub
   Private Sub timer1_timer()
   Call radpoint
   '在计时器控制下产生小点
   End Sub
   Sub radpoint()
   r = Rnd * 255
   g = Rnd * 255
   xpos = Rnd * ScaleWidth
   ypos = Rnd * ScaleHeight
   PSet (xpos, ypos), RGB(r, 255, 0)
   '设置小点的颜色和发生位置
   If Picture1.Left >= form1.Width Then
   form1.Cls
   Picture1.Left = Rnd * form1.Width: Picture1.Top = Rnd * form1.Height
   form1.Show
   End If
   End Sub
   Private Sub form_keypress(keycode As Integer)
   temp = ShowCursor(1)
   '当键盘按下时,显示鼠标光标
   Unload Me '退出程序
   End Sub
   Private Sub form_mousedown(button As Integer, shift As Integer, x As Single, y As Single)
   temp = ShowCursor(1)
   Unload Me
   '鼠标按下时,退出
   End Sub
   Private Sub form_mousemove(button As Integer, shift As Integer, x As Single, y As Single)
   Static xlast, ylast As Single
   Dim xnow, ynow As Single
   '确定鼠标移动前后的坐标变量,通过比较两者之间的差来判断鼠标的移动
   xnow = x:ynow = y
   If xlast = 0 And ylast = 0 Then
   xlast = xnow: ylast = ynow
   Exit Sub
   End If
   If Abs(xnow - xlast) > 1 Or Abs(ynow - ylast) > 1 Then
   temp = ShowCursor(1)
   Unload Me
   '鼠标移动后,退出屏保
   End If
   End Sub
   此程序尚未添加有关屏保退出时的密码和屏保音效设置方面的代码,熟悉VB的朋友可以自行编写(再另加一两个窗体)。