通过VBA编实现PPT中图片的拖动功能

此处建议将宏的安全级别设置为低。

1.在演示文稿插入图像控件(视图——工具栏——控件工具箱),打开属性窗口,将picture设成你想拖动的图片(图片大小要根据实际情况进行调整),遗憾的是Image控件不支持透明。
2.双击图像控件,打开的VBA编辑窗口(注意双击后先删除所有代码),复制下面的代码:

Dim X1, Y1 As Integer
Dim Down As Boolean
Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Not Down Then
X1 = X
Y1 = Y
Down = True
End If
End Sub
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Down Then
Image1.Left = Image1.Left + X - X1
Image1.Top = Image1.Top + Y - Y1
X1 = X
Y1 = Y
End If
End Sub
Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Down = False
SlideShowWindows(1).View.First
End Sub

3.保存,关闭VBA编辑窗口,放映幻灯片,即可看效果了。
4.如果你想拖动多张图片,如法炮制,复制上面的三个鼠标事件,修改Image1、X1、Y1。

拖动图片方式:播放PPT,在图片上按住鼠标,并且要长按住,这样图片才会随你鼠标移动,放开后,图片就停在你放开的地方。

Last modification:December 28th, 2019 at 11:37 am
If you think my article is useful to you, please feel free to appreciate