有时我们需要在PPT实现一些简单的交互:比如鼠标悬停在某个对象上,该对象进行放大,鼠标移开后对象恢复原尺寸。这种交互一般使用VBA实现。接下来,我用一个简单的缩放交互示例进行演示。

开始操作

1.1 新建或打开PowerPoint演示文稿

新建或打开PowerPoint演示文稿

1.2 调出开发工具选项卡

PPT → 文件 → 选项 → 自定义功能区 → 勾选开发工具 → 确定

1.3 另存为pptm

按Ctrl+Shift+S快捷键 → 保存类型:启用宏的PowerPoint演示文稿 → 确定

2.1 查看代码

开发工具选项卡 → 查看代码

2.2 插入模块

选中VBAProject右键 → 插入 → 模块

2.3 复制如下代码

Private oldshp As Shape ​ Sub ZoomShape(ByVal shp As Shape) Dim mw As Single Dim mh As Single If oldshp Is Nothing Then mw = shp.Left + shp.Width / 2 mh = shp.Top + shp.Height / 2 shp.Width = shp.Width * 1.2 shp.Height = shp.Height * 1.2 shp.Left = mw - shp.Width / 2 shp.Top = mh - shp.Height / 2 Set oldshp = shp End If End Sub ​ Sub Restore() Dim mw As Single Dim mh As Single If Not oldshp Is Nothing Then mw = oldshp.Left + oldshp.Width / 2 mh = oldshp.Top + oldshp.Height / 2 oldshp.Width = oldshp.Width / 1.2 oldshp.Height = oldshp.Height / 1.2 oldshp.Left = mw - oldshp.Width / 2 oldshp.Top = mh - oldshp.Height / 2 Set oldshp = Nothing End If End Sub

2.4 粘贴代码后关闭窗口

粘贴代码

3.1 图形添加动作

选中图形 → 插入选项卡 → 动作 → 鼠标悬停 → 运行宏:ZoomShape → 确定

3.2 插入全屏矩形

插入一个矩形 → 手动调整尺寸与幻灯片一致 → 透明度:100% → 将全屏矩形置于底层3.3 全屏

3.3 全屏矩形添加动作

选中全屏矩形 → 插入选项卡 → 动作 → 鼠标悬停 → 运行宏:Restore → 确定

3.4 按Shift+F5放映该页幻灯片即可查看效果

鼠标移到方块上,方块的尺寸变为1.2倍。鼠标移开,方块恢复为原尺寸

最后

本示例实现的是对象从中心进行缩放的交互。代码框架如下:

Private oldshp As Shape ​ Sub ZoomShape(ByVal shp As Shape) If oldshp Is Nothing Then '在此输入对象状态改变代码 Set oldshp = shp End If End Sub ​ Sub Restore() If Not oldshp Is Nothing Then '在此输入对象状态恢复代码 Set oldshp = Nothing End If End Sub

在此代码框架下,有VBA基础的朋友也可以尝试去实现图形的透明度、文字加粗、图形显隐性等效果的交互。