零起点学VB做外挂(四)

时间:2021-11-11 01:40:06

   前一段,我们学了基本的键盘模拟部分,但是对于只是前台的模拟,大家应该不会满足吧,那么今天我们要做的就是后台模拟。呵呵,很企盼吧!

  后台模拟,就要用到postmessage或者sendmessage,大家可以google下他们的详细资料。我这里先放上他们的声明吧:

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam 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
这个函数可以利用窗口名来找到句柄,而句柄就是我们进行下一步的关键了。
我们拿记事本为例来写个小程序吧,工具是VB6.0:

Private Sub keytest_Click()
        hWndGt = FindWindow(vbNullString, "无标题 - 记事本")

        Label13.Caption = ""
        Label13.Caption = "记事本母窗口句柄:0x" & Hex(hWndGt)  '利用label13显示记事本句柄
        
        WaitingSeconds (0.3)
        childhwnd = FindWindowEx(hWndGt, vbNullString, "Edit", "") 
        Label14.Caption = ""
        Label14.Caption = "记事本子窗口句柄:0x" & Hex(childhwnd)'利用label14显示记事本的子窗口“edit”的句柄
        WaitingSeconds (0.3)

        Dim i As Integer
        looptime = 1
       
       
        For i = 1 To looptime Step 1

            PostMessage childhwnd, WM_KEYDOWN, VK_I, 0&  '输出I
            PostMessage childhwnd, WM_KEYDOWN, VK_SPACE, 0&  '输出space
            PostMessage childhwnd, WM_KEYDOWN, VK_SPACE, 0&  '输出space
            WaitingSeconds (0.5)
            PostMessage childhwnd, WM_KEYDOWN, VK_L, 0& '输出L
            PostMessage childhwnd, WM_KEYDOWN, VK_O, 0& '输出O
            PostMessage childhwnd, WM_KEYDOWN, VK_V, 0& '输出V
            PostMessage childhwnd, WM_KEYDOWN, VK_E, 0& '输出E
            PostMessage childhwnd, WM_KEYDOWN, VK_SPACE, 0&  '输出space
            PostMessage childhwnd, WM_KEYDOWN, VK_SPACE, 0&  '输出space
            WaitingSeconds (0.5)
            PostMessage childhwnd, WM_KEYDOWN, VK_Y, 0& '输出Y
            PostMessage childhwnd, WM_KEYDOWN, VK_O, 0& '输出O
            PostMessage childhwnd, WM_KEYDOWN, VK_U, 0& '输出U
            PostMessage childhwnd, WM_KEYDOWN, VK_ENTER, 0&  '输出enter
           
            WaitingSeconds (0.5)
        Next

        
        Label15(0).Caption = ""
        Label15(0).Caption = "记事本发送完成"

End Sub
上边这段程序是这样做得:先找到记事本的窗口,但是由于记事本的特点,它的母窗口并不能让我们进行操作,只有它的第一个子窗口“Edit”才是我们可以进行操作的。所以我们又利用函数FindWindowEx找到它的第一个子窗口“Edit”的句柄。然后在Edit窗口进行了模拟按键的输出“I LOVE YOU”。

无论记事本是在前台,还是后台,甚至最小化的时候,我们依然可以利用上面的函数将“I LOVE YOU”输入进记事本,是不是很感慨呢。现在我们已经初步完成了模拟按键的学习。

无论前台还是后台,我们都能做到!  外挂,我们来了!