PowerPoint(或Excel)VBA捕获鼠标单击的坐标 [英] PowerPoint (or Excel) VBA Capture Coordinates of Mouse Click

查看:996
本文介绍了PowerPoint(或Excel)VBA捕获鼠标单击的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些背景

迅速的背景是,我正处于为PowerPoint构建外接程序的研究阶段.我的最终目标是开发一个CAD尺寸标注插件,以帮助加快工程演示文稿的创建.我们必须做很多"PowerPoint工程",在这些零件的简化版本上显示零件的一般尺寸,这些零件是用PPT形状或CAD几何图形本身的屏幕截图创建的.但是,一遍又一遍地创建尺寸是很乏味的.每一个通常由一个箭头,两行和一个带有尺寸值的文本框组成.

The quick background is that I am in the research stages of building an add-in for PowerPoint. My end goal is to develop a CAD Dimensioning Add-in to help expedite the creating of Engineering Presentations. We have to do a lot of "PowerPoint Engineering" where the general sizes of components are shown on simplified versions of said components created with PPT shapes or screenshots of the CAD geometry itself. But creating dimensions over and over is tedious. Each one generally consists of an arrow, 2 lines, and a text box with the dimension value.

这是我需要帮助的地方.

(如果您知道如何在Excel中执行以下操作,那么该方法也将起作用,稍后我将介绍等效的PPT.)

(If you know how to do the following in Excel, that would work too and I will work to figure the PPT equivalent later.)

在PowerPoint幻灯片中,在设计模式下(即不在幻灯片放映模式下),我想执行以下工作流程:

In a PowerPoint slide, while in design mode (i.e. not Slide Show Mode), I want to do the following workflow:

  1. 在打开的用户窗体中,用户单击名为"START"的按钮
  2. 代码开始在幻灯片本身的字段中侦听鼠标左键(LMC)(它不应响应实际UserForm上的LMC,例如,如果用户需要将UserForm拖出方式)
  3. 在LMC上,光标的坐标记录为(x1,y1)
  4. 重复步骤2和3来记录(x2,y2)
  5. 使用这些坐标进行操作(例如,在两个坐标之间绘制尺寸

我相信除了第2步外,我可以处理所有编码,这就是我在这里的原因.我很难找到起点.具体来说,我需要有关如何收听LMC 的帮助.换句话说,以下是什么构想:

I believe I can handle all of the coding with the exception step 2, which is why I am here. I am having much trouble finding a starting point. Specifically, I need help with how to listen for a LMC. In words, what envision is the following:

While Listening:
  If LMC = TRUE
    Do Stuff
  End If
End While 

但是我没有编码While Listening部分的知识.我需要朝正确的方向轻推.

But I don't have the knowledge to code the While Listening part. I need a nudge in the right direction.

我的搜索使我进入了MSDN上的MouseDown事件处理程序页面,但是在我的测试中,我认为这不是我所需要的.似乎MouseDown旨在在鼠标按下自身的用户窗体中的CommandButton时启动例程.

My searches have landed me on the MouseDown event handler pages at MSDN, but in my testing, I don't think that this is what I need. Its seem as though MouseDown is intended to start a routine when the Mouse is Down on a CommandButton in the UserForm it self.

我还发现了这样的帖子,唯一的答案似乎暗示着如果不加长篇幅就不可能做到这一点,并且代码可能对文件本身有害:

I have also found this SO post where the only answer seemed to imply that this was not possible without going to great lengths AND the code was possible detrimental to the file itself: How to record mouse clicks in Excel VBA?. (I have no problem going to great lengths and putting in the work, but not if the resulting code has a high likelihood of doing damage as the post seems to suggest.) (Also, the OP was downvoted with no explanation, maybe someone can tell me why so I don't make the same mistake.)

推荐答案

您可以通过执行以下操作来完成您想要做的事情(底部可能对您最有帮助): 首先,声明以下内容:

You can accomplish what you are looking to do by doing the following (the bottom part may be the most helpful to you): First, Declare the following:

Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10

和以下代码段将允许您单击,双击或右键单击:

and the following code snippets will allow you do either click, double click, or right click:

Private Sub SingleClick()
  SetCursorPos 100, 100 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub DoubleClick()
  'Double click as a quick series of two clicks
  SetCursorPos 100, 100 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub RightClick()
  Right Click
  SetCursorPos 200, 200 'x and y position
  mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub

您需要做的就是将光标位置更改为屏幕上的坐标. 为此,我使用以下代码创建了一个新的宏,并将其分配给"Ctrl + Y"按钮.这将告诉您当前鼠标位置的坐标.

All you need to do is change the cursor position to the coordinates on the screen. To do this, I made a new macro with the following code and assigned it to the "Ctrl+Y" button. This will tell you the coordinates of your current mouse location.

Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
    x As Long
    y As Long
End Type

Sub CurosrXY_Pixels()

    Dim lngStatus As Long
    Dim typWhere As POINTAPI

    lngStatus = GetCursorPos(typWhere)
    MsgBox "x: " & typWhere.x & Chr(13) & "y: " & typWhere.y, vbInformation, "Pixels"

End Sub

这篇关于PowerPoint(或Excel)VBA捕获鼠标单击的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆