[VB.NET]简单动画

简单动画

实例说明

在本实例中,我们将制作一个能实现简单动画功能的应用程序。程序运行后,可通过不断改变两个图片实现动画效果。程序运行结果如图77-1所示。

图77-1 运行结果

技术要点

l 切换图片

l Timer控件的使用

实现过程

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"SimpleDynPic",然后选择保存路径。单击"确认"。

■ 添加控件

向当前窗体上添加一个Timer控件,一个Button按钮,一个Label控件和两个Picture控件。

■ 设置属性

对窗体上的控件设置属性,详细情况如表77-1所示。

表77-1 窗体及控件的属性值

窗体/控件 属性 值

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

Timer1 Enable True

Interval 1000

Form1 Text 简单动画

■ 添加代码

Dim flap As Short

Private Sub butterfly()

' 在两个位图间切换.

If flap = 0 Then

btrfly.Image = btrfly1.Image

flap = 1

Else

btrfly.Image = btrfly2.Image

flap = 0

End If

End Sub

Private Sub Okay_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Okay.Click

infoform.DefInstance.Close()

End Sub

Private Sub Timer2_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer2.Tick

' 可以更改Timer控件的时间间隔属性来决定动画的快慢

butterfly()

End Sub

■ 运行程序

单击菜单"调试|启动"或单击 图标运行程序。

小结

本实例通过使用Timer控件在两个图片之间不断切换以实现动画的效果。

基于VB的文字动画特效代码Private Sub TextEffect( _ ByVal sText As String, _ ByVal lX As Long, ByVal lY As Long, _ Optional ByVal bLoop As Boolean = False, _ Optional ByVal lStartSpacing As Long = 128, _ Optional ByVal lEndSpacing As Long = -1, _ Optional ByVal oColor As OLE_COLOR = vbWindowText _ ) Dim i As Long Dim x As Long Dim lLen As Long Dim lHDC As Long Dim hBrush As Long '定义各种变量 Static tR As RECT Dim iDir As Long Dim bNotFirstTime As Boolean Dim lTime As Long Dim lIter As Long Dim bSlowDown As Boolean Dim lCOlor As Long Dim bDoIt As Boolean iDir = -1 i = lStartSpacing '为变量赋值 tR.left = lX: tR.tOp = lY: tR.Right = lX: tR.Bottom = lY OleTranslateColor oColor, 0, lCOlor hBrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)) lLen = Len(sText) lHDC = Me.hdc SetTextColor lHDC, lCOlor '设置文字颜色 bDoIt = True Do While m_bDoEffect And bDoIt lTime = timeGetTime If (i < -3) And Not (bLoop) And Not (bSlowDown) Then bSlowDown = True iDir = 1 lIter = (i + 4) End If If (i > 128) Then iDir = -1 If Not (bLoop) And iDir = 1 Then If (i = lEndSpacing) Then bDoIt = False Else lIter = lIter - 1 If (lIter <= 0) Then i = i + iDir lIter = (i + 4) End If End If Else i = i + iDir End If FillRect lHDC, tR, hBrush '调用FillRect函数 x = 32 - (i * lLen) SetTextCharacterExtra lHDC, i DrawText lHDC, sText, lLen, tR, DT_CALCRECT '调用API函数DrawText tR.Right = tR.Right + 4 If (tR.Right > Me.ScaleWidth \ Screen.TwipsPerPixelX) Then tR.Right = Me.ScaleWidth \ Screen.TwipsPerPixelX DrawText lHDC, sText, lLen, tR, DT_LEFT Me.Refresh '窗体刷新 Do DoEvents '后台运行 Loop While (timeGetTime - lTime) < 20 Loop DeleteObject hBrush End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值