Solved

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

Posted on 2011-09-08
3
790 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36505872
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 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36505892
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
ID: 36505958
LOL... I hate it when that happens  =)  Thanks IM!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

738 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