schneider said:
I need to hook the system mouse down event.
I'm trying to replicate how a context menu hides when the mouse clicks
outside of the control.
Thanks,
Schneider
I'm assuming you want to be able to get the mouse anywhere on the screen.
If you only mean in your app, then you would handle the mouse events for
your form - otherwise you can look at this sample
This needs a lot more
work to be complete - but here is a sample of installing a global mouse
hook. The work that needs to be done is in the HookProc in the
GlobalMouseHook class. You can use the information that is passed to
actually send information to the event (that's why I used the
mouseeventhandler). You also have access to mouse wheel info if you so
desire. Anyway, here is a basic example, watch for wrap
Option Strict On
Option Explicit On
Option Infer Off
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
End Sub
#Region "Designer Code"
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.IntegralHeight = False
Me.ListBox1.Location = New System.Drawing.Point(0, 0)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(284, 264)
Me.ListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 264)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
#End Region
Private WithEvents gmh As GlobalMouseHook
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
gmh = New GlobalMouseHook()
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
gmh.Dispose()
End Sub
Private Sub gmh_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles gmh.MouseDown
Me.ListBox1.Items.Add(String.Format("{0} down at screen coordinate
({1},{2})", e.Button, e.X, e.Y))
End Sub
Private Sub gmh_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles gmh.MouseMove
'Me.ListBox1.Items.Add("Move")
End Sub
Private Sub gmh_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles gmh.MouseUp
Me.ListBox1.Items.Add("Up")
End Sub
End Class
Friend Class GlobalMouseHook
Implements IDisposable
Private hhk As IntPtr = IntPtr.Zero
Private disposedValue As Boolean = False ' To detect redundant
calls
Public Event MouseDown As MouseEventHandler
Public Event MouseUp As MouseEventHandler
Public Event MouseMove As MouseEventHandler
Public Sub New()
Hook()
End Sub
Private Sub Hook()
Dim hInstance As IntPtr = LoadLibrary("User32")
hhk = SetWindowsHookEx(WH_MOUSE_LL, AddressOf Me.HookProc,
hInstance, 0)
End Sub
Private Sub Unhook()
UnhookWindowsHookEx(hhk)
End Sub
Private Function HookProc(ByVal nCode As Integer, ByVal wParam As
UInteger, ByRef lParam As MSLLHOOKSTRUCT) As Integer
If nCode >= 0 Then
Select Case wParam
Case WM_LBUTTONDOWN
RaiseEvent MouseDown(Me, New
MouseEventArgs(MouseButtons.Left, 0, lParam.pt.x, lParam.pt.y, 0))
Case WM_RBUTTONDOWN
RaiseEvent MouseDown(Me, New
MouseEventArgs(MouseButtons.Right, 0, lParam.pt.x, lParam.pt.y, 0))
Case WM_MBUTTONDOWN
RaiseEvent MouseDown(Me, New
MouseEventArgs(MouseButtons.Middle, 0, lParam.pt.x, lParam.pt.y, 0))
Case WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP
RaiseEvent MouseUp(Nothing, Nothing)
Case WM_MOUSEMOVE
RaiseEvent MouseMove(Nothing, Nothing)
Case WM_MOUSEWHEEL, WM_MOUSEHWHEEL
Case Else
Console.WriteLine(wParam)
End Select
End If
Return CallNextHookEx(hhk, nCode, wParam, lParam)
End Function
#Region "API Declarations"
<StructLayout(LayoutKind.Sequential)> _
Private Structure API_POINT
Public x As Integer
Public y As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure MSLLHOOKSTRUCT
Public pt As API_POINT
Public mouseData As UInteger
Public flags As UInteger
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
Private Const WM_MOUSEWHEEL As UInteger = &H20A
Private Const WM_MOUSEHWHEEL As UInteger = &H20E
Private Const WM_MOUSEMOVE As UInteger = &H200
Private Const WM_LBUTTONDOWN As UInteger = &H201
Private Const WM_LBUTTONUP As UInteger = &H202
Private Const WM_MBUTTONDOWN As UInteger = &H207
Private Const WM_MBUTTONUP As UInteger = &H208
Private Const WM_RBUTTONDOWN As UInteger = &H204
Private Const WM_RBUTTONUP As UInteger = &H205
Private Const WH_MOUSE_LL As Integer = 14
Private Delegate Function LowLevelMouseHookProc(ByVal nCode As Integer,
ByVal wParam As UInteger, ByRef lParam As MSLLHOOKSTRUCT) As Integer
Private Declare Auto Function LoadLibrary Lib "kernel32" (ByVal
lpFileName As String) As IntPtr
Private Declare Auto Function SetWindowsHookEx Lib "user32.dll" (ByVal
idHook As Integer, ByVal lpfn As LowLevelMouseHookProc, ByVal hInstance As
IntPtr, ByVal dwThreadId As UInteger) As IntPtr
Private Declare Function CallNextHookEx Lib "user32" (ByVal hhk As
IntPtr, ByVal nCode As Integer, ByVal wParam As UInteger, ByRef lParam As
MSLLHOOKSTRUCT) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As
IntPtr) As Boolean
#End Region
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free other state (managed objects).
End If
Unhook()
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable
pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class