如何在vba中用SendMessage向窗口发送消息?

api函数SendMessage可以向指定的窗口发送消息,直到这个消息被处理才返回。

以下代码将向光标所在的窗口发送关闭窗口的消息WM_CLOSE

Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Type POINTAPI
    X As Long
    Y As Long
End Type
Const WM_CLOSE = &H10
Sub QQ1722187970()
    Dim i As POINTAPI
    GetCursorPos i
    Debug.Print i.X, i.Y
    Dim lHwnd As Long
    lHwnd = WindowFromPoint(i.X, i.Y)
    SendMessage lHwnd, WM_CLOSE, 0, 0
End Sub

 

       

发表评论