如何在vba中用WindowFromPoint获得光标所在的窗口句柄?

api函数WindowFromPoint可以获得点所在的窗口的句柄。

api函数GetCursorPos可以获得鼠标光标所在的坐标。

结合以上两点,可以获得鼠标所在的窗口的句柄。

以下代码将获得当前鼠标所在的窗口的句柄:

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Type POINTAPI
X As Long
Y As Long
End Type
Sub xyf()
    Dim i As POINTAPI
    GetCursorPos i
    Debug.Print i.X, i.Y
    Dim lHwnd As Long
    lHwnd = WindowFromPoint(i.X, i.Y)
    MsgBox lHwnd
End Sub

 

       

发表评论