vb6.0 hook 基础代码

模块代码

  Option Explicit
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
    (ByVal idHook As Long, _
    ByVal lpfn As Long, _
    ByVal hmod As Long, _
    ByVal dwThreadId As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hhook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhook As Long) As Long

Private Type KBDLLHOOKSTRUCT
    VKCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type

'idHook代表是何种Hook,有以下几种
Public Const WH_CALLWNDPROC = 4
Public Const WH_CALLWNDPROCRET = 12
Public Const WH_CBT = 5
Public Const WH_DEBUG = 9
Public Const WH_FOREGROUNDIDLE = 11
Public Const WH_GETMESSAGE = 3
Public Const WH_HARDWARE = 8
Public Const WH_JOURNALPLAYBACK = 1
Public Const WH_JOURNALRECORD = 0
Public Const WH_KEYBOARD = 2
Public Const WH_MOUSE = 7
Public Const WH_MSGFILTER = (-1)
Public Const WH_SHELL = 10
Public Const WH_SYSMSGFILTER = 6
Public hhook As Long

Public Function MyKBHFunc(ByVal iCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    MyKBHFunc = 0 '表示要处理这个讯息
    If wParam = vbKeyEscape Then         '侦测 有没有按到PrintScreen键
        MyKBHFunc = 1 '在这个Hook便吃掉这个讯息
    End If
    Call CallNextHookEx(hhook, iCode, wParam, lParam) '传给下一个Hook
End Function

窗体代码

Option Explicit
'设置hook
Private Sub Form_Activate()
    hhook = SetWindowsHookEx(2, AddressOf MyKBHFunc, 0, App.ThreadID)
End Sub
'卸载
Private Sub Form_Unload(Cancel As Integer)
    UnhookWindowsHookEx hhook
End Sub

详情请参考另一篇文章 VB HOOK详细介绍

你可能感兴趣的:(vb6.0 hook 基础代码)