Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine when a mouse hook has handled the last message
    text
    copied!<p>I have a form, that acts like a drop-down, that I display non-modal. I attach a mouse hook to the form to determine when the mouse is clicked out of it, so that I know to close it - by setting <code>Visible = False</code>.</p> <p>Because I want the <code>HookProc</code> to handle the last click, I can't dispose the Hook or my Dropdown until I'm sure that my event handler has returned to the HookProc.</p> <p>It's a bit hard to explain, but I hope the code below makes it a little clearer:-</p> <pre><code>//Loop to display the dropdown. Dim dd as New DropDown dd.Visible = True Do While dd.Visible Application.DoEvents() NativeMethods.MsgWaitForMultipleObjectsEx(0, IntPtr.Zero, 250, &amp;HFF, 4) Loop // I want to dispose dd now, but how can I be sure that e.Handled (See below) // has been returned to HookProc? //A handler within dropdown to determine what to do with the mouse click. Private Sub DropDown_MouseHookClick(ByVal sender As Object, ByVal e As MouseClickEventArgs) If IWantToCloseTheDropDown Then e.Handled = True MyHook.UnHook Me.Visible = False End If // All done, e.Handled is returned to HookProc. // But which happens first? Will e.Handled arrive at HookProc first, or will // the form display loop, above, notice that Visible is now False? End Sub //The main part of the hooking class. Public Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer Dim MyMouseHookStruct As MouseHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(MouseHookStruct)), MouseHookStruct) If nCode &lt; 0 Then Return CallNextHookEx(hHook, nCode, wParam, lParam) Else Dim handle As Integer = MyMouseHookStruct.hwnd Dim c As Control = Control.FromHandle(New IntPtr(handle)) If MouseUpOrDown Then Dim e As MouseHookClickEventArgs OnMouseClick(e) If e.Handled Then Return 1 EndIf End If Return CallNextHookEx(hHook, nCode, wParam, lParam) End If End Function </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload