| 
     利用VB5.0和WINDOWS95自身所带的“画笔”就可以制作出带有动画和声音的屏幕保护程序。画面是一只蝴蝶在音乐的伴奏下飞动,碰动屏幕的边缘就会向小球反弹一样换个方向飞。这里需要两个bmp文件,采用的是VB自带的文件(源文件的位置是c:program filesdevstduiovbsamplespguidevcrfly1.bmp和bfly2.bmp)。因为蝴蝶要向各个方向飞,所以还要在这两个文件的基础上,再制作两个。打开WINDOWS附件中的“画笔”,打开bfly1.bmp,点取“画像”菜单中的“翻转/旋转”,选择“水平翻转”后,将生成的新文件另存存盘,起名“左飞”,同样的方法将bfly2.bmp另存为“左合”,将bfly1.bmp改名为“右飞”,bfly2.bmp改名为“右合”(左飞,左合是蝴蝶向左飞时的画面,右飞,右合是蝴蝶向右飞时的画面,最后在画笔中将4个图的底色都改为黑色,这是保证蝴蝶的底色和屏幕的背景相融合)。 动画的制作是利用image控件来实现的。改变image的picture属性可实现画面的变化,利用move命令可实现画面的移动。利用MCI控件来实现音乐播放。程序编译后将生成的exe可执行文件,改为以*.scr结尾的文件名,将其复制到windowssystem子目录即可,然后就可以在“控制面板”的“显示器”中选择该屏幕保护程序即可使用(运行所带exe文件必须将mid复制到指定的位置。这里建议使用所带的setup进行自动安装,可以省去一切步骤,setup后的文件名为蝴蝶.scr,已自动安装到系统的文件夹,在“显示器”中的“屏幕保护程序”可以找到。重新编译程序所需的4个bmp文件和一个Mid文件也安装到指定的位置。详细路径请见源程序中的说明)。
 具体做法如下:首先建立一个新窗体form1,各属性分别是backcolor选为黑色,borderstyle选为0(黑色不带边框)。在form1上加入7个image控件,在格式菜单中将各个image的大小都选为相同,image1-7的stretch属性都选为true(图像将随控件的大小而变化),image1-6的visible的属性选为false(不可见),image7的visible属性为true。
 Image1.picture=右飞 Image2.picture =右合
 Image3.picture=左飞 Image4.picture =左合
 在“工程”菜单中选择“部件”,并从“部件”对话框的“控件”列表中选择“Microsoft Multimedia Control5.0”加入MCI控件,并将其visible属性改为false。加入时钟控件timer1,其interval属性为200(interval的值与机器的配置有关,在其他机器上可作适当调整,我的配置是P133,内存为32M)。
 原理如下:image1-4用来存图形,运行时不可见。Image5用来存蝴蝶翅膀张开时的图像,Image6用来存蝴蝶翅膀合上时的图像,运行时都不可见。Image7是唯一运行时可见,利用时钟控件来改变其图形,用Image5和Image6来回变换。
 If Image7.Picture = Image5.Picture Then
 Image7.Picture = Image6.Picture
 Else
 Image7.Picture = Image5.Picture
 End If
 蝴蝶的移动利用image7控件的move方法:
 image7.move xp,yp
 4个边界值
 上边:y=0
 下边:y=form1.scaleheight-image7.scaleheight
 左边:x=0
 右边:x= form1.scalewidth-image7.scalewidth
 xp,yp为水平和垂直的移动量。
 x,y为image7在窗体中的位置
 左右边界的判断:
 If x>=form1.ScaleWidth-Image1.Width Then \'右边界
 Image5.Picture = Image3.Picture
 \'蝴蝶应向左飞,翅膀张开的图形应选为左飞
 Image6.Picture = Image4.Picture
 \'翅膀合上的图形应选为左合
 x = Form1.ScaleHeight - Image1.Width
 \'改变 x 的位置
 xp = (-1) * xp
 \'改变水平的移动量符号,以便向相反方向飞。
 End If
 If x <= 0 Then \'左边界
 Image5.Picture = Image1.Picture \'右飞
 Image6. Picture = Image2. Picture \'右合
 x = 0
 xp = (-1) * xp
 End If
 If y > = Form1.ScaleHeight - Image1. Height Then
 \'下边界
 y = Form1. ScaleHeight - Image1. Height
 \'改变y的位置
 yp = (-l) * yp
 \'改变垂直的移动量符号,以便向相反方向飞
 End If
 If y <= 0 Then \'上边界
 y = 0
 yp = (-1) * yp
 End If
 声音的播出:这里为播放mid文件
 MMControl1. DeviceType =\"\"
 MMControl1. filename = \"c:mideine.mid\" (eine.mid 为一 mid 文件)
 MMControl1. Command = \"open\"
 MMControl1. Command = \"play\"
 这还需利用API函数实现鼠标的隐藏与出现,这里就不一一介绍了。
     部分程序代码如下(详细代码及解释请见源文件): 
 Dim x As Integer Dim y As Integer
 Dim xp As Integer
 Dim yp As Integer
 Dim lastx, lasty
 private Sub Form1_KeyDown(KeyCode As integer, Shift As Integer)
 Endscrnsave \'结束屏幕保护程序
 End Sub
 Private Sub Form1_Load()
 Move 0, 0,Screen. Width,Screen. Height \'让form1全屏显示
 hidemouse \'隐藏鼠标
 x = l000 \'蝴蝶的开始位置
 y = l000
 xp = 80 \'移动量
 yp = 80
 Image5. picture = Imagel. Picture
 \'确定翅膀张开的图形是“右飞”,蝴蝶向右飞
 Image6. Picture = Image2. Picture
 \'确定翅膀合上的图形是“右合”
 Image7. Picture = Image1. Picture
 MMControl1. Command = \"close\" \'确保MCI控件已关闭
 MMControl1. DeviceType =\"\"
 \'启动程序就播放mid 声音文件
 MMControl1.filename = \"c:mideine.mid\"
 MMControl1.Command = \"open\"
 MMControl1.Command = \"play\"
 End Sub
 Private Sub Form1_MouseMove(Button As integer, Shift As Integer, x As Single, y As Single )
 If IsEmpty (lastx) Or IsEmpty (lasty) Then
 lastx = x
 lasty = y
 End If
 If Abs(lastx - x) > 2 Or Abs(lasty - y) > 2 Then
 endscrnsave
 End If
 lastx = x
 lasty = y
 End Sub
 Private Sub Form1_Unload (Cancel As integer)
 MMControl1. Command = \"close\"
 Unload Me
 End Sub
 Private Sub Timer1_Timer()
 \'判断mid文件是否播放完,如果播放完,进行重播
 If MMControl1. Poesition = 895 Then
 \'些mid文件的长度(mid文件的长度,可建立一Label控件,在时钟控件中令
 \'Lagel1.caption=mmcontrol1.position,就可动态显示播放mid
 \'文件的位置,当mid播放完,就可求出此mid文件的长度。)
 MMControl1. Command = \"prev\"
 \'回到此mid文件的开始位置
 MMControl1. Command = \"play\" \'播放
 End If
 x = x + xp \'增加移动量
 y = y + yp
 If x > = Form1. ScaleWidth - Image1. Width Then
 \'右边界判断
 Image5. Picture = Image3. Picture \'换图像
 Image6. Picture = Image4. Picture
 x = Form1. ScaleHeight - Image1. Width
 xp = (-1) * xp \'改变移动量
 End If
 If x < =0 Then
 Image5.Picture = Image1. Picture
 Image6.Picture = Imege2. Picture
 x = 0
 xp = (-1) * xp
 End If
 If y > = Form1.ScaleHeight - Image1. Height Then
 y = Form1. ScaleHeight - Image1. Height
 yp = (-1) * yp
 End If
 If y <= O Then
 y = O
 yp = (-1) * yp
 End If
 If Image7.Picture = Image5. Picture Then
 \'不断改变图象以实现翅膀的一张一合。
 Image7. Picture = Image6. Picture
 Else
 Image7. Picture = Image5. Picture
 End If
 Image7. Move x, y \'最关键的一步蝴蝶的移动
 End Sub
 模块中的代码:
 Declare Function ShowCursor Lib \"user32\" (ByVal bSbow As Long) As Long
 Sub endscrnsave() \'结束此程序
 showmouse
 End
 End Sub
 Sub showmouse()
 While ShowCursor(True) < 0
 Wend
 End Sub
 Sub Hidemouse() \'隐藏鼠标
 While ShowCursor(False) > = 0
 Wend
 End Sub
 sub Main()
 If App. PrevInstance = True Then
 Exit Sub
 End If
 Form1. Show
 End Sub
   |