英文VB教程 简体Visual Basic教程 繁体Visual Basic教程

第二十九课: 创建动画



动画程序设计始终是有趣和令人兴奋的。虽然 Visual Basic 不是专门设计动画的程序语言,但你仍可以建立一些有趣的动画效果。VB6有很多方法可以创造动画效果 ,我们先学习应用一些较简单的方法。

创建动画最简单的方法是通过触发一组事件,如点击一个按钮来设置一组图像或图片或文本和标签为真或假的Visible属性。让我们看看下面的例子:

这个程序制造一只海鸥飞向4个方向的错覺。为了做到这一点,我们插入5只海鸥图像到表单中。把中心的图像Visible属性设置为True,其余图像的Visible属性则设置为FALSE 。当程序运行时,用户将只能够看到中间的图。接下来,插入5个命令按钮,并把它们的标签改成向北, 向东, 向西,向南和返回。在下列程序双击向北按钮然后键入以下的程序:

'制造向北飞的假象
Private Sub cmdNorth_Click()
Image1.Visible = False
Image2.Visible = True
Image3.Visible = False
Image4.Visible = False
Image5.Visible = False
End Sub

 

当用户按一下向北按钮,只有图像3被显示,这将造成一个海鸥飞向北方的错觉。其余的按钮也以同样的方法键入类似的程序。

Private Sub cmdEast_Click()

‘向东
Image1.Visible = False
Image2.Visible = False
Image3.Visible = True
Image4.Visible = False
Image5.Visible = False
End Sub

Private Sub cmdSouth_Click()

‘向南
Image1.Visible = False
Image2.Visible = False
Image3.Visible = False
Image4.Visible = True
Image5.Visible = False
End Sub

Private Sub cmdWest_Click()

’向西
Image1.Visible = False
Image2.Visible = False
Image3.Visible = False
Image4.Visible = False
Image5.Visible = True
End Sub
 

返回按钮的程序代码如下:

Private Sub cmdBack_Click()
Image1.Visible = True
Image2.Visible = False
Image3.Visible = False
Image4.Visible = False
Image5.Visible = False
End Sub

除此之外,为了使用户在程序启动时只看到中间的海鸥,你必须添加以下的程序:

Private Sub Form_Load)
Image1.Visible = True
Image2.Visible = False
Image3.Visible = False
Image4.Visible = False
Image5.Visible = False
End Sub

 

 

图29.1

您也可使用一个本文框来发出命令,这个想法其实是来自我的儿子刘迅( 10岁) 。

图29.2

在文本框中,当您键入北字时,海鸥将飞向北方。当您键入西字时,海鸥将飞向西方。当您键入南字时,海鸥将飞向南方。当您键入东字时,海鸥将飞向东方。当您键入返回时,海鸥将飞回原位。

输出屏幕如下:

图29.3

他的程序代码如下:

程序代码

Private Sub Command1_Click()

If Text1.Text = "北" Then
Image1.Visible = False
Image2.Visible = True
Image3.Visible = False
Image4.Visible = False
Image5.Visible = False
ElseIf Text1.Text = "东" Then
Image1.Visible = False
Image3.Visible = True
Image2.Visible = False
Image4.Visible = False
Image5.Visible = False
ElseIf Text1.Text = "西" Then
Image1.Visible = False
Image3.Visible = False
Image2.Visible = False
Image4.Visible = False
Image5.Visible = True
ElseIf Text1.Text = "南" Then
Image1.Visible = False
Image3.Visible = False
Image2.Visible = False
Image4.Visible = True
Image5.Visible = False
ElseIf Text1.Text = "返回" Then
Image1.Visible = True
Image3.Visible = False
Image2.Visible = False
Image4.Visible = False
Image5.Visible = False

End If
End Sub  

另一种简单的模拟动画方法是利用VB6中的Left 和Top 的属性。 Image.left代表 图像左边和屏幕左边界之间的距离,而Image.top代表图像顶部和屏幕上部边界的距离。距离是以Twip作为单位,一个Twip 相等于1 / 1440英寸。使用如Image.left - 100 的陈述句将使图像向左移100个Twip的距离, Image.top + 100的陈述句将使图像向右移100个Twip的距离, Image.top -100的陈述句将使图像向上移100个Twip的距离,Image.top+100的陈述句将使图像向下移100个Twip的距离 。

下面这个程序使您每次点击一个有关命令按钮时可以把物体向上,下,左,右移动。该守则,如image1.top = image1.top + 100 ,是使用户每按一下命令按钮使距离增加或减少。例如,如果图像的原来位置是离上方边界1000Twip,当用户按一下按钮时,它和上方的距离将增加到1100Twip,再按一下则它的距离将是1200Twip 等等。因此,所有四个按钮皆可用类似的程序来编写。这样,按一下四个按钮的任何一个可使该图像移动四个方向。

图29.4

程序代码

Private Sub Command1_Click()
Image1.Top = Image1.Top + 100
End Sub

Private Sub Command2_Click()
Image1.Top = Image1.Top - 100
End Sub

Private Sub Command3_Click()
Image1.Left = Image1.Left + 100
End Sub

Private Sub Command4_Click()
Image1.Left = Image1.Left - 100
End Sub

 

第四个例子可以让使用者放大和缩小图像或对象。要达到这个效果,我们可通过改变对象的高度和宽度属性。这是类似上面的例子。陈述句image1.height = image1.height + 100和image1.width = image1.width + 100将使每一次用户按一下有关命令按钮时,对象就增加 100Twip 的高度和宽度。在另一方面,陈述句image1.height = image1.height -1 00和i mage1.width= i mage1.width- 100将使每一次用户按一下有关命令按钮时,对象就减少100Twip 的高度和宽度

 

图29.5 

程序代码

Private Sub Command1_Click()
Image1.Height = Image1.Height + 100
Image1.Width = Image1.Width + 100
End Sub

Private Sub Command2_Click()

Image1.Height = Image1.Height - 100
Image1.Width = Image1.Width - 100

End Sub





版权所有©2008 Dr.Liew Voon Kiong。保留所有权利 。联系我们: VB面子书

[Privacy Policy]