Solved

Stop Click Event Within Low Level Mouse Hook - VB.NET

Posted on 2011-09-08
3
743 Views
Last Modified: 2012-05-12
Good afternoon folks.

I'm at the last stage of a piece I'm adding to a project of mine and running into a slight snag. I have a low level mouse hook being implemented to intercept the left mouse button down event taking place on another form. Everything works great except for one thing: The click event depresses the button that I'm monitoring. I can't allow that button to actually be depressed.

I was thinking that within my hook I could simply return 0 at the point where I catch the mouse down event and that would be it... apparently it's not that simple or I'm overlooking something stupid =)  

I've attached the full working class that hooks the mouse and sets my boolean property. Where you see the line Return 0 ... my hope was that the event would be sent to garbage collection and not actually depress the button they clicked.

Many thanks!!!
Public Class clsMouseHook
    Private Structure MSLLHOOKSTRUCT
        Public pt As Point
        Public mouseData As Int32
        Public flags As Int32
        Public time As Int32
        Public extra As IntPtr
    End Structure
    Public hndl As Integer
    Public p_btnClicked As Boolean = False
    Private _mouseHook As IntPtr
    Private Const WH_MOUSE_LL As Int32 = 14
    Private Delegate Function MouseHookDelegate(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32 '<MarshalAs(UnmanagedType.FunctionPtr)>
    Private _mouseProc As MouseHookDelegate
    Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As MouseHookDelegate, ByVal hInstance As IntPtr, ByVal wParam As Int32) As IntPtr
    Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hook As IntPtr) As Boolean
    Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
    Private Declare Function GetModuleHandleW Lib "kernel32.dll" (ByVal fakezero As IntPtr) As IntPtr
    Public Declare Function WindowFromPoint Lib "user32" (ByVal pt As PointAPI) As IntPtr
    Public Structure PointAPI
        Public X As Integer
        Public Y As Integer
        Public Sub New(ByVal pt As Point)
            X = pt.X
            Y = pt.Y
        End Sub
    End Structure


    Public Function HookMouse(ByVal btnHndl As Integer) As Boolean
        If _mouseHook = IntPtr.Zero Then
            _mouseProc = New MouseHookDelegate(AddressOf MouseHookProc)
            _mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
        End If
        hndl = btnHndl 'stores the button handle that was passed in during the hook process
        Return _mouseHook <> IntPtr.Zero
    End Function

    Public Sub UnHookMouse()
        If _mouseHook = IntPtr.Zero Then Return
        UnhookWindowsHookEx(_mouseHook)
        hndl = 0
        _mouseHook = IntPtr.Zero
    End Sub

    Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
        Dim clkHndl As IntPtr
        If wParam.ToInt32 = 513 Then 'interrupt the left mouse button event here with 513
            If hndl <> 0 Then 'check to make sure we have a button handle
                'need to now determine if where the left click took place is on the button's location
                'clkHndl = sc.GetHandleFromLocation(lParam.pt.Y, lParam.pt.X)
                clkHndl = WindowFromPoint(New PointAPI(Cursor.Position))
                ' ... now use clkHndl to find out if it matches the stored button windows handle
                If clkHndl = hndl Then
                    BtnClicked = True
                    Return 0
                End If
            End If
        End If
        Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
    End Function
    Public Property BtnClicked() As Boolean
        Get
            Return p_btnClicked
        End Get
        Set(ByVal value As Boolean)
            p_btnClicked = value
        End Set
    End Property

End Class

Open in new window

0
Comment
Question by:romieb69
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Pretty sure you need to return 1 instead of 0 (one instead of zero).

Change:

                    Return 0

To:

                    Return 1 ' <-- Suppress mouse event
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
See LowLevelMouseProc Callback Function:
http://msdn.microsoft.com/en-us/library/ms644986(VS.85).aspx

    "If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure."
0
 

Author Comment

by:romieb69
Comment Utility
LOL... I hate it when that happens  =)  Thanks IM!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now