全局钩子函数,只能存在于WINDOWS DLL中。而这些DLL只能使用VC或者C++Builder等工具来编写。VB是做不出来的。至于SetWindowHookEx的使用方法,建议你看看MSDN,上面写得很清楚。其实使用这个函数,并不是很复杂。

解决方案 »

  1.   

    如果你有msdn的话,用hook进行搜索,找到是visual basci 主题的
    那里或许对你有很大的帮助
    这只是其中的一篇MORE INFORMATIONSteps to Reproduce Behavior
    Create a new Standard EXE project with Visual Basic 6.0. Form1 is created by default. 
    Under Project, select Add module to add a new standard module and Add User Control to add a new user control to the project. 
    Add the following code to the standard module:Option ExplicitPublic Declare Function CallNextHookEx Lib "user32" _
       (ByVal hHook As Long, _
       ByVal nCode As Long, _
       ByVal wParam As Long, _
       ByVal lParam As Long) As LongPublic Declare Function UnhookWindowsHookEx Lib "user32" _
       (ByVal hHook As Long) As LongPublic Declare Function SetWindowsHookEx Lib "user32" _
       Alias "SetWindowsHookExA" _
       (ByVal idHook As Long, _
       ByVal lpfn As Long, _
       ByVal hmod As Long, _
       ByVal dwThreadId As Long) As LongPublic Const WH_MOUSE = 7
    Public Const HC_ACTION = 0
    Public Const WM_RBUTTONDOWN = &H204Public hHook As LongPublic Function MouseProc(ByVal nCode As Long, ByVal wParam As Long, _
                                  ByVal lParam As Long) As Long
       If nCode >= 0 Then
          If nCode = HC_ACTION And wParam = WM_RBUTTONDOWN Then
             MsgBox "get WM_RBUTTONDOWN "
          End If
       End If
       MouseProc = CallNextHookEx(hHook, nCode, wParam, lParam)
    End Function
     
    Put the following code into the code window of the User Control module:Option ExplicitPrivate Sub UserControl_Initialize()
       hHook = SetWindowsHookEx(WH_MOUSE, _
                AddressOf MouseProc, 0&, App.ThreadID)
    End SubPrivate Sub UserControl_Terminate()
       Call UnhookWindowsHookEx(hHook)
    End SubPublic Sub ResetHook()
       Call UnhookWindowsHookEx(hHook)
       hHook = SetWindowsHookEx(WH_MOUSE, _
               AddressOf MouseProc, 0&, App.ThreadID)
    End Sub 
    Close the user control design window and put UserControl1 on the form. 
    Build the project and run the EXE file generated. Right-click on the form and note that nothing happens. Close the form to exit. NOTE: If you run the application in the Visual Basic IDE, make sure you do not click the Stop button. Always unload the form to exit the application, otherwise you will get a General Protection Fault (GPF). 
    Steps to Fix the Problem
    With the project created above, add the following code to the code window of Form1:Option ExplicitPrivate Sub Form_Load()
       UserControl11.ResetHook
    End Sub 
    Recompile the project and run the EXE again. This time, whenever you right-click on the form, you get a pop-up message box. REFERENCES
    For additional information using hooks with UserControls, click the article number below to view the article in the Microsoft Knowledge Base: