Problem with MOUSEHOOKSTRUCT

Goggy

Member
Joined
May 10, 2006
Messages
8
According to msdn...(if i read it rite..)... the MOUSEHOOKSTRUCT should return the handle of the window thats recieving the message....
well it doesnt seem to return anything....
could someone shine some light on this for me?
Thank u all 4 your help.
Code:
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading
Imports System.String

Public Class LLMouse

#Region "Api Declarations"
    ---------------------------------------------------------------------------
    A VB.6 long converts to a VB.net integer
    ---------------------------------------------------------------------------
    Private Declare Function UnhookWindowsHookEx Lib "user32" _
                      (ByVal hHook As Integer) As Integer

    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" ( _
                        ByVal idHook As Integer, _
                        ByVal lpfn As MouseHookDelegate, _
                        ByVal hmod As Integer, _
                        ByVal dwThreadId As Integer) As Integer

    Private Declare Function CallNextHookEx Lib "user32" _
                        (ByVal hHook As Integer, _
                        ByVal nCode As Integer, _
                        ByVal wParam As Integer, _
                        ByVal lParam As MOUSEHOOKSTRUCTEX) As Integer

    Private Declare Function GetFocus Lib "user32" Alias "GetFocus" () As Integer

    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
                        ByVal hwnd As Integer, _
                        ByVal lpString As String, _
                        ByVal cch As Long) As Integer

    Private Declare Function WindowFromPoint Lib "user32" Alias "WindowFromPoint" ( _
                        ByVal xPoint As Integer, _
                        ByVal yPoint As Integer) As Integer

    Private Declare Function FlashWindow Lib "user32" Alias "FlashWindow" ( _
                        ByVal hwnd As Integer, _
                        ByVal bInvert As Integer) _
                        As Integer
#End Region

#Region "Mouse Structure"

    Public Structure MOUSEHOOKSTRUCT
        Public POINT As POINT
        Public hwnd As Integer
        Public wHitTestCode As Integer
        Public dwExtraInfo As Integer
    End Structure
    Public Structure MOUSEHOOKSTRUCTEX
        Public MOUSEHOOKSTRUCT As MOUSEHOOKSTRUCT
        Public mouseData As Integer
    End Structure

#End Region

#Region "Mouse Constants"

    Private Const HC_ACTION = 0

    Private Const WM_MOUSEACTIVATE = &H21
    private Const WM_MOUSEFIRST = &H200
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    Private Const WM_LBUTTONDBLCLK = &H203

    Private Const WH_MOUSE_LL = 14&

#End Region

    Private MouseHandle As Integer

    Public Delegate Function MouseHookDelegate( _
                            ByVal Code As Integer, _
                            ByVal wParam As Integer, _
                            ByRef lParam As MOUSEHOOKSTRUCTEX) _
                            As Integer

    <MarshalAs(UnmanagedType.FunctionPtr)> _
    Private callback As MouseHookDelegate

    Public Sub HookMouse()
        callback = New MouseHookDelegate(AddressOf MouseCallback)

        MouseHandle = SetWindowsHookEx( _
                        WH_MOUSE_LL, _
                        callback, _
                        Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, _
                        0)
    End Sub

    Public Function MouseCallback( _
                        ByVal Code As Integer, _
                        ByVal wParam As Integer, _
                        ByRef lParam As MOUSEHOOKSTRUCTEX) As Integer

        If (Code = HC_ACTION) Then

            Select Case wParam
                Case Else
                    Dim Window_Name As String
                    Window_Name = Space(32)

                    GetWindowText( _
                        WindowFromPoint( _
                                lParam.MOUSEHOOKSTRUCT.POINT.X, _
                                lParam.MOUSEHOOKSTRUCT.POINT.Y), _
                        Window_Name, _
                        Window_Name.Length)

                    Window_Name = Left(Window_Name, InStr(Window_Name, Chr(0), CompareMethod.Binary) - 1)
                    If Window_Name.Length <> 0 Then
                        Debug.Write(Hex(wParam) & vbCrLf)
                        Debug.Write(Window_Name.Length & vbCrLf)
                        Debug.Write(Window_Name & vbCrLf)
                        Debug.Write(lParam.MOUSEHOOKSTRUCT.hwnd & vbCrLf)
                        Debug.Write(lParam.mouseData.ToString & vbCrLf)
                    End If
                    If lParam.MOUSEHOOKSTRUCT.hwnd <> 0 Then FlashWindow(lParam.MOUSEHOOKSTRUCT.hwnd, True)
            End Select

        End If


        Return CallNextHookEx( _
                        MouseHandle, _
                        Code, _
                        wParam, _
                        lParam)

    End Function

    Public Sub UnhookMouse()

        Call UnhookWindowsHookEx(MouseHandle)

    End Sub
End Class
 
Back
Top