Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, _
lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
然后,对于获取mouseX和mouseY的子例程,将其放在下面的某个地方:
Function MouseX(Optional ByVal hWnd As Long) As Long
' Get mouse X coordinates in pixels
'
' If a window handle is passed, the result is relative to the client area
' of that window, otherwise the result is relative to the screen
Dim lpPoint As POINTAPI
Application.Volatile(false)
GetCursorPos lpPoint
If hWnd Then ScreenToClient hWnd, lpPoint
MouseX = lpPoint.X
End Function
和/或
Function MouseY(Optional ByVal hWnd As Long) As Long
' Get mouse Y coordinates in pixels
'
' If a window handle is passed, the result is relative to the client area
' of that window, otherwise the result is relative to the screen
Dim lpPoint As POINTAPI
Application.Volatile(false)
GetCursorPos lpPoint
If hWnd Then ScreenToClient hWnd, lpPoint
MouseY = lpPoint.Y
End Function
2条答案
按热度按时间pxy2qtax1#
嗯,它并不是完全内置在AFAIK中,但我发现this page给出了一个适合我的建议:
在一个模块中,把这个放在顶部:
然后,对于获取mouseX和mouseY的子例程,将其放在下面的某个地方:
和/或
然后,在Excel中,如果您简单地输入单元格
=mouseX()
,当您点击ENTER
时,它将返回mouseX位置。=mouseY()
也一样。试了一下,我做到了:
并让它工作。
编辑:注意,我不像VBA中的其他东西那样擅长图表,所以当你创建图表时,你需要将
.Shapes("Chart 1").
部分编辑为你所使用的任何图表名称/编号。或者通过迭代。z0qdvdin2#
不确定鼠标x y,但你可以得到工作表选择范围的变化。将图表放在该位置。