新浪博客

Powerpoint动画三板斧之VBA程序

2016-04-19 08:19阅读:
实例:怎样利用vba制作路径——完全非弹性碰撞动画
  ppt的路径动画无法精确调整路径端点,因此本例用vba解决这一问题。
 1、要做的效果是:左边的球中心运动到(9cm,10cm)时,两球立刻以原来速度的一半向右滑出画面。因此,需要对m1设置两段动画——碰撞前的运动较快,碰撞后的运动较慢,m2设置一段动画——但要和m1的第二段一起执行。
 2、先把幻灯片页面设为32cm宽,进入vba环境,编写下面的程序。
Public Sub AddMotionPath()
Dim m1 As Shape, m2 As Shape, effNew As Effect, aniMotion As AnimationBehavior

'绘制两个小球,并分别设置填充色
Set m1 = ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoShapeOval, _
Left:=cm2p(1), Top:=cm2p(10), Width:=cm2p(1), Height:=cm2p(1))
m1.Fill.ForeColor.RGB = RGB(255, 0, 0)
Set m2 = ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoShapeOval, _
Left:=cm2p(12), Top:=cm2p(10),
Width:=cm2p(1), Height:=cm2p(1))
m2.Fill.ForeColor.RGB
= RGB(0, 0, 255)
'设置左边小球碰撞前的动画效果
Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
AddEffect(Shape:=m1, effectId:=msoAnimEffectPathRight, _

Trigger:=msoAnimTriggerOnPageClick)
Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
aniMotion.MotionEffect.Path = 'M 0 0 L ' & 10 / 32 & ' 0'
aniMotion.Timing.Speed = 1

'设置左边小球碰撞后的动画效果
Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
AddEffect(Shape:=m1, effectId:=msoAnimEffectPathRight, _

Trigger:=msoAnimTriggerAfterPrevious)
Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
aniMotion.MotionEffect.Path = 'M ' & 10 / 32 & ' 0 L 1 0'
aniMotion.Timing.Speed = 0.5

'设置右边小球碰撞后的动画效果
Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
AddEffect(Shape:=m2, effectId:=msoAnimEffectPathRight, _

Trigger:=msoAnimTriggerWithPrevious)
Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
aniMotion.MotionEffect.Path = 'M 0 0 L ' & 22 / 32 & ' 0'
aniMotion.Timing.Speed = 0.5
End Sub

  程序一开始绘制两个小圆形,直径均为1cm,单位需要转化为磅,cm2p()函数见下面。
Private Function cm2p(cm As Single) As Single
cm2p = cm * 28.35
End Function

  然后,程序添加3段向右运动的效果,AddEffect方法的Shape参数是指定应用动画的对象,effectID指定动画效果,Trigger指定动画发生时间(这里是单击幻灯片页面时发生),后面定义路径(字母M定义起点,M 0 0就是表示对象当前位置为起点,L表示直线运动到的位置,L 10/32 0表示横向向右运动幻灯片宽度的10/32,另外还有C、Z、X、H、V命令),再定义速度。以下定义碰撞以后m1和m2的运动效果,语句大体不变,只改变动画发生时间(m1的为前一动画结束时,m2与m1的第二段动画同时)和速度。
  以上程序只需要执行一遍即设置完毕,然后可以删掉语句。

我的更多文章

下载客户端阅读体验更佳

APP专享