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

Author: Sunny Date: 2001年 33期

?牐犜谇懊妫颐茄?习了如何在Windows 98中实现Windows XP的某些个性化功能和一些有关系统、软件的个性化设置。那么,对于我们编程爱好者而言,能够自己动手制作才算“真英雄”。OK,就请你跟着来一起制作动画屏保吧,相信你一定能从中学到不少好东西!
  ?牐犖颐侵溃琖indows系统中的屏幕保护程序扩展名为.scr,千万别以为这是微软的什么秘密文件格式,它只不过是一种在系统控制下的可执行文件(.exe文件)被换名存放在C盘Windows目录下罢了。因此,用VB的动画设计方法也可以做出同样精彩的屏保来。现在,就让我们来一起动手,在你的屏幕上做个“星空”吧!本例的重点在于学习在VB中如何控制图片。
  ?牐犉舳疺B 6.0后,新建一个工程,在窗体上放置三个计时器,timer1的interval值设为90,timer2 和timer3的interval值均设为500,再设置两个图片组:Picture2到picture9为一组,Picture0和picture1为一组。其中,除picture1和picture2的可视化属性(visible)设为TRUE外,其它图片的visible属性一律设为FALSE。
  ?牐牶昧耍衷谒鞔疤澹慈胍韵麓耄?
  ?牐燩rivate Declare Function ShowCursor Lib“user32”(ByVal bShow As Long)As Long
  ?燩rivate Declare Function SystemParametersInfo Lib “user32” Alias “systemparametersinfoa” () As Long
  ?牐燚im z As Integer
  ?牐燩rivate Sub form_load()
  ?牐爐emp = ShowCursor(0)
  ?牐牐б厥蟊?
  ?牐燚rawWidth = 1
  ?牐牐г诒尘吧匣〉?
  ?牐燩icture1.Left = 0: Picture1.Top = 0.3 * form1.Width
  ?牐燩icture1.Width = 600: Picture1.Height = 800
  ?牐爖 = 0
  ?牐燛nd Sub
  ?牐燩rivate Sub timer2_timer()
  ?牐燬elect Case z
  ?牐燙ase 0
  ?牐燩icture2.Picture = Picture3.Picture: z = 1
  ?牐燙ase 1
  ?牐燩icture2.Picture = Picture2.Picture: z = 2
  ?牐燙ase 2
  ?牐燩icture2.Picture = Picture5.Picture: z = 3
  ?牐燙ase 3
  ?牐燩icture2.Picture = Picture6.Picture: z = 4
  ?牐燙ase 4
  ?牐燩icture2.Picture = Picture8.Picture: z = 5
  ?牐燙ase 5
  ?牐燩icture2.Picture = Picture9.Picture: z = 6
  ?牐爖 = 0
  ?牐燛nd Select
  ?牐牐Ы惶嫦允就计纬啥Ч?
  ?牐燜or i = 0 To 100
  ?牐爄 = i + 1
  ?牐燦ext
  ?牐燩icture2.Left = Picture2.Left + i
  ?牐牐?通过不断增加图片的左边距来达到移动图片的目的
  ?牐營f Picture2.Left >= form1.Width Then
  ?牐燩icture2.Left = 0
  ?牐燩icture2.Top = Rnd * form1.Height
  ?牐爁orm1.Cls
  ?牐爁orm1.Show
  ?牐牐?设置图片移出界面后的处理方法:图片回到左边界,清屏后再显示背景
  ?牐燛nd If
  ?牐燛nd Sub
  ?牐燩rivate Sub timer3_timer()
  ?牐燬elect Case z
  ?牐燙ase 0
  ?牐燩icture1.Picture = Picture7.Picture: z = 1
  ?牐燙ase 1
  ?牐燩icture1.Picture = Picture0.Picture: z = 2
  ?牐燙ase 2
  ?牐燩icture1.Picture = Picture7.Picture: z = 3
  ?牐爖 = 0
  ?牐燛nd Select
  ?牐燜or i = 1 To 90
  ?牐爄 = i + 1
  ?牐燦ext
  ?牐燩icture1.Left = Picture1.Left + i
  ?牐燩icture1.Top = Picture1.Top + 0.5 * i
  ?牐營f Picture1.Left = 0 Or Picture1.Left >= form1.Width - Picture1.Width Then
  ?牐燩icture1.Left = 0
  ?牐爁orm1.Cls
  ?牐爁orm1.Show
  ?牐燛nd If
  ?牐營f Picture1.Top = 0 Or Picture1.Top>= form1.Height Then
  ?牐爁orm1.Cls
  ?牐爁orm1.Show
  ?牐燩icture1.Top = Rnd * form1.Height + i
  ?牐燛nd If
  ?牐燛nd Sub
  ?牐燩rivate Sub timer1_timer()
  ?牐燙all radpoint
  ?牐牐г诩剖逼骺刂葡虏〉?
  ?牐燛nd Sub
  ?牐燬ub radpoint()
  ?牐爎 = Rnd * 255
  ?牐爂 = Rnd * 255
  ?牐爔pos = Rnd * ScaleWidth
  ?牐爕pos = Rnd * ScaleHeight
  ?牐燩Set (xpos, ypos), RGB(r, 255, 0)
  ?牐牐?设置小点的颜色和发生位置
  ?牐營f Picture1.Left >= form1.Width Then
  ?牐爁orm1.Cls
  ?牐燩icture1.Left = Rnd * form1.Width: Picture1.Top = Rnd * form1.Height
  ?牐爁orm1.Show
  ?牐燛nd If
  ?牐燛nd Sub
  ?牐燩rivate Sub form_keypress(keycode As Integer)
  ?牐爐emp = ShowCursor(1)
  ?牐牐У奔贪聪率保允臼蟊旯獗?
  ?牐燯nload Me '退出程序
  ?牐燛nd Sub
  ?牐燩rivate Sub form_mousedown(button As Integer, shift As Integer, x As Single, y As Single)
  ?牐爐emp = ShowCursor(1)
  ?牐燯nload Me
  ?牐牐?鼠标按下时,退出
  ?牐燛nd Sub
  ?牐燩rivate Sub form_mousemove(button As Integer, shift As Integer, x As Single, y As Single)
  ?牐燬tatic xlast, ylast As Single
  ?牐燚im xnow, ynow As Single
  ?牐牐?确定鼠标移动前后的坐标变量,通过比较两者之间的差来判断鼠标的移动
  ?牐爔now = x:ynow = y
  ?牐營f xlast = 0 And ylast = 0 Then
  ?牐爔last = xnow: ylast = ynow
  ?牐燛xit Sub
  ?牐燛nd If
  ?牐營f Abs(xnow - xlast) > 1 Or Abs(ynow - ylast) > 1 Then
  ?牐爐emp = ShowCursor(1)
  ?牐燯nload Me
  ?牐牐?鼠标移动后,退出屏保
  ?牐燛nd If
  ?牐燛nd Sub
  ?牐牬顺绦蛏形刺砑佑泄仄帘M顺鍪钡拿苈牒推帘R粜?设置方面的代码,熟悉VB的朋友可以自行编写(再另加一两个窗体)。