ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 898|回复: 15

[求助] VBA如何判断点击了哪个图片

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-2-29 22:49 | 显示全部楼层 |阅读模式
如题,详见附件:

VBA如何判断点击了哪个图片.rar (478.77 KB, 下载次数: 30)
原代码仅对提前待定情况下有效,请老师指点一下如何修改,谢谢!

TA的精华主题

TA的得分主题

发表于 2024-3-1 09:27 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 kevinchengcw 于 2024-3-1 13:11 编辑

见楼下帖子。。。。。。

TA的精华主题

TA的得分主题

发表于 2024-3-1 09:29 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-1 17:01 | 显示全部楼层
kevinchengcw 发表于 2024-3-1 09:29
可以用类模块解决

非常感谢,经测试,改成提示:
    If Sel.Type = ppSelectionShapes Then MsgBox Sel.SlideRange.Name & " - " & Sel.ShapeRange.Name
在非播放状态下没问题。
但在播放状态下,却无任何反应;对图片插入任一个宏,也无反应,请问下是哪里问题呢(注:Office2007版)

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-3 20:04 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
顶起一下,
实在不行本想用 Image 控件,无奈有的图形特性无法都用控件取代......
能否在 OnSlideShowPageChange 或其他方法实现呢?
播放时能判断点击不同的图片(/图形),以作出不同的处理

TA的精华主题

TA的得分主题

发表于 2024-3-5 23:54 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
aman1516 发表于 2024-3-3 20:04
顶起一下,
实在不行本想用 Image 控件,无奈有的图形特性无法都用控件取代......
能否在 OnSlideShowPag ...
  1. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  2. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
  3. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  4. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
  5. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

  6. Private Type POINTAPI
  7.    x As Long
  8.    y As Long
  9. End Type

  10. Private Type Rectangle
  11.     topLeft As POINTAPI
  12.     bottomRight As POINTAPI
  13. End Type

  14. Private Function TransformShape(osh As Shape) As Rectangle
  15.     Dim zoomFactor As Double
  16.     On Error Resume Next
  17.     zoomFactor = ActivePresentation.slideShowWindow.View.Zoom / 100

  18.     Dim hndDC&
  19.     hndDC = GetDC(0)
  20.     Dim deviceCapsX As Double
  21.     deviceCapsX = GetDeviceCaps(hndDC, 88) / 72 ' pixels per pt horizontal (1 pt = 1/72')
  22.     Dim deviceCapsY As Double
  23.     deviceCapsY = GetDeviceCaps(hndDC, 90) / 72 ' pixels per pt vertical (1 pt = 1/72')

  24.     With TransformShape
  25.         ' calculate:
  26.         .topLeft.x = osh.Left * deviceCapsX * zoomFactor
  27.         .topLeft.y = osh.Top * deviceCapsY * zoomFactor
  28.         .bottomRight.x = (osh.Left + osh.Width) * deviceCapsX * zoomFactor
  29.         .bottomRight.y = (osh.Top + osh.Height) * deviceCapsY * zoomFactor
  30.         ' translate:
  31.         Dim lngStatus As Long
  32.         lngStatus = ClientToScreen(hndDC, .topLeft)
  33.         lngStatus = ClientToScreen(hndDC, .bottomRight)
  34.     End With

  35.     ReleaseDC 0, hndDC
  36. End Function


  37. Sub SlideShape_Click()
  38.     Dim pointerPos As POINTAPI
  39.     Dim shapeAsRect As Rectangle
  40.     Dim sld As Slide
  41.     Dim shp As Shape
  42.     Dim p As Long
  43.     Application.SlideShowWindows(1).View.Slide.Select '选中播放的幻灯片
  44.     p = Application.ActiveWindow.View.Slide.slideIndex '获取当前幻灯片编号
  45.     Set sld = ActivePresentation.Slides(p) '指定要操作的幻灯片页数(这里为播放状态的那张)
  46.     For Each shp In sld.Shapes
  47.         If shp.Type = msoPicture Then '判断形状类型是否为图片
  48.             shapeAsRect = TransformShape(shp)
  49.             GetCursorPos pointerPos '获取鼠标指针坐标
  50.             If ((pointerPos.x <= shapeAsRect.topLeft.x) Or (pointerPos.x >= shapeAsRect.bottomRight.x) Or _
  51.                 (pointerPos.y <= shapeAsRect.topLeft.y) Or (pointerPos.y >= shapeAsRect.bottomRight.y)) Then
  52.                 ' 图片不在鼠标所点区域
  53.             Else ' 图片在鼠标所点区域
  54.                 MsgBox "你点击了第" & p & "页的“" & shp.Name & "”图片!"
  55.                 Exit For
  56.             End If
  57.         End If
  58.     Next
  59. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2024-3-6 12:19 | 显示全部楼层
本帖最后由 shenjianrong163 于 2024-3-6 12:57 编辑

郁闷!只有WPS才可用。修改如下:

  1. Private Type POINTAPI
  2.    x As Long
  3.    y As Long
  4. End Type

  5. Private Type Rectangle
  6.     topLeft As POINTAPI
  7.     bottomRight As POINTAPI
  8. End Type

  9. #If Win64 And VBA7 Then
  10.     Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  11.     Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
  12.     Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  13.     Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
  14.     Private Declare PtrSafe Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
  15. #Else
  16.     Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  17.     Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
  18.     Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  19.     Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
  20.     Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
  21. #End If

  22. Private Function TransformShape(osh As Shape) As Rectangle
  23.     Dim zoomFactor As Double
  24.     On Error Resume Next
  25.     zoomFactor = ActivePresentation.slideShowWindow.View.Zoom / 100

  26.     Dim hndDC&
  27.     hndDC = GetDC(0)
  28.     Dim deviceCapsX As Double
  29.     deviceCapsX = GetDeviceCaps(hndDC, 88) / 72 ' pixels per pt horizontal (1 pt = 1/72')
  30.     Dim deviceCapsY As Double
  31.     deviceCapsY = GetDeviceCaps(hndDC, 90) / 72 ' pixels per pt vertical (1 pt = 1/72')

  32.     With TransformShape
  33.         ' calculate:
  34.         .topLeft.x = osh.Left * deviceCapsX * zoomFactor
  35.         .topLeft.y = osh.Top * deviceCapsY * zoomFactor
  36.         .bottomRight.x = (osh.Left + osh.Width) * deviceCapsX * zoomFactor
  37.         .bottomRight.y = (osh.Top + osh.Height) * deviceCapsY * zoomFactor
  38.         ' translate:
  39.         Dim lngStatus As Long
  40.         lngStatus = ClientToScreen(hndDC, .topLeft)
  41.         lngStatus = ClientToScreen(hndDC, .bottomRight)
  42.     End With

  43.     ReleaseDC 0, hndDC
  44. End Function


  45. Sub SlideShape_Click()
  46.     Dim pointerPos As POINTAPI
  47.     Dim shapeAsRect As Rectangle
  48.     Dim sld As Slide
  49.     Dim shp As Shape
  50.     Dim p As Long
  51.     On Error Resume Next
  52.     p = SlideShowWindows(1).View.Slide.slideIndex '获取当前播放的幻灯片编号
  53.     Set sld = ActivePresentation.Slides(p) '指定要操作的幻灯片页数(这里为播放状态的那张)
  54.     For Each shp In sld.Shapes
  55.         'If shp.Type = msoPicture Then '判断形状类型是否为图片(WPS适用)
  56.             shapeAsRect = TransformShape(shp)
  57.             GetCursorPos pointerPos '获取鼠标指针坐标
  58.             If ((pointerPos.x <= shapeAsRect.topLeft.x) Or (pointerPos.x >= shapeAsRect.bottomRight.x) Or _
  59.                 (pointerPos.y <= shapeAsRect.topLeft.y) Or (pointerPos.y >= shapeAsRect.bottomRight.y)) Then
  60.                 ' 图片不在鼠标所点区域
  61.             Else ' 图片在鼠标所点区域
  62.                 MsgBox "你点击了第" & p & "页的“" & shp.Name & "”图片!"
  63.                 Exit For
  64.             End If
  65.         'End If
  66.     Next
  67. End Sub

复制代码


TA的精华主题

TA的得分主题

发表于 2024-3-6 13:15 | 显示全部楼层
附件供参考:


VBA如何判断点击了哪个图片.rar (475.77 KB, 下载次数: 20)

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-3-6 18:38 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 perfect131 于 2024-3-6 18:45 编辑

原来是这样

1.png
2.png
4.gif

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-6 19:51 | 显示全部楼层

非常感谢!
太强大了,这个用坐标识别的好处,是稍作修改还可以在播放地随意拖拽图片
好好学习!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-18 09:37 , Processed in 0.049573 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表