VB的动画实现原理 曾代劲 2001年 51期 大家一定看见过很多软件的图标都是动态显示的吧,有的还在软件的界面上有动画。今天我们就教大家用Visual Basic中的Timer控件做一些简单的动画,原理就是用多幅图片不停地轮换形成动画。   新建一个工程,在窗体上添加两个Timer控件,三个Image控件,然后在代码窗口中添加以下代码:   Dim i As Integer   Dim j As Integer   Dim k As Integer   Dim l As Integer   Private Sub Form_Load   '让Image1和Image2重叠   Image2.Left = Image1.Left   Image2.Top = Image1.Top   '此处的图片路径为相对路径   Image1.Picture = LoadPicture(“C:\Program Files\Tencent\newface\57-1.bmp”)   Image2.Picture = LoadPicture(“C:\Program Files\Tencent\newface\57-3.bmp”)   Image3.Picture = LoadPicture(“C:\Program Files\Tencent\newface\1-1.bmp”)   '设置Timer1的时间间隔,并在Form1的Load事件中激活   Timer1.Interval = 10   Timer1.Enabled = True   '设置Timer2的时间间隔,并在Form1的Load事件中激活   Timer2.Interval = 1000   Timer2.Enabled = True   End Sub   Private Sub Timer1_Timer()   '动画过程,图片的路径为相对路径   Select Case i   Case 0   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon01.ICO”)   Case 1   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon02.ICO”)   Case 2   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon03.ICO”)   Case 3   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon04.ICO”)   Case 4   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon05.ICO”)   Case 5   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon06.ICO”)   Case 6   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon07.ICO”)   Case 7   Form1.Icon = LoadPicture(“C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Elements\moon08.ICO”)   End Select   '改变引号内的字符串以在标题栏显示不同的标题   Form1.Caption = Left(“Wolf”, j)   Image3.Left = 240 + l   i = i + 1   j = j + 1   l = l + 1   'If 语句用来控制变量i和j的最大值   If i = 7 Then i = 0   If j = 5 Then j = 0   End Sub   Private Sub Timer2_Timer   '用If 语句来控制图像框的Visible的属性以达到动画效果   If k = 0 Then   Image1.Visible = False   Image2.Visible = True   Else   Image1.Visible = True   Image2.Visible = False   End If   k = k + 1   'If语句用来控制变量k的最大值   If k = 2 Then k = 0   End Sub   以上的图片在你的硬盘里都能找到,当然,你也可以用你喜欢的图片来替换它们。   现在按F5运行,是不是动起来了啊?窗体上的图标在动,标题也在动,小企鹅从左到右移动,还有一个卡通人物眼晴眨啊眨的,是不是很有趣啊?你可以修改Timer的Interval属性来控制动画的速度,其实这只是一些简单动画的演示实例,要做出更加漂亮的动画还需要大家努力喔。