如何用api关闭QQ好友的聊天窗口?

如果要用vba关闭QQ好友的聊天窗口,需要借助Api函数。

这里介绍两种方法。

方法一:用CloseWindow函数关闭QQ好友的聊天窗口

代码如下:

Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseWindow Lib "user32" (ByVal hwnd 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 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Const WM_CLOSE = &H10
Sub QQ1722187970()
    Dim sQQ As String
    Dim iHwnd As Long
    sQQ = "3462565771"
    '打开QQ好友聊天窗口
    sCmd = "cmd /c start tencent://Message/?Uin=" & sQQ
    Shell sCmd
    For i = 1 To 30000
        DoEvents
    Next i
    '查找句柄
    iHwnd = FindWindow("TXGuiFoundation", "QQ昵称")
    '关闭QQ好友聊天窗口
    CloseWindow iHwnd
End Sub

方法二:用SendMessage函数发送WM_CLOSE消息进行关闭。

代码如下:

Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseWindow Lib "user32" (ByVal hwnd 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 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Const WM_CLOSE = &H10
Sub QQ1722187970()
    Dim sQQ As String
    Dim iHwnd As Long
    sQQ = "3462565771"
    '打开QQ好友聊天窗口
    sCmd = "cmd /c start tencent://Message/?Uin=" & sQQ
    Shell sCmd
    For i = 1 To 30000
        DoEvents
    Next i
    '查找句柄
    iHwnd = FindWindow("TXGuiFoundation", "QQ昵称")
    '关闭QQ好友聊天窗口
     SendMessage iHwnd, WM_CLOSE, 0, 0
End Sub

总结:
方法二对于任意的QQ版本均可适用,方法一在某些QQ版本上不可行。

       

发表评论