"schneider" <es********@com munity.nospamwr ote in message
news:Oc******** ******@TK2MSFTN GP02.phx.gbl...
>
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
mouseeventhandl er). 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()
InitializeCompo nent()
End Sub
#Region "Designer Code"
'Form overrides dispose to clean up the component list.
<System.Diagnos tics.DebuggerNo nUserCode()_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Disp ose()
End If
Finally
MyBase.Dispose( disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()_
Private Sub InitializeCompo nent()
Me.ListBox1 = New System.Windows. Forms.ListBox
Me.SuspendLayou t()
'
'ListBox1
'
Me.ListBox1.Doc k = System.Windows. Forms.DockStyle .Fill
Me.ListBox1.For mattingEnabled = True
Me.ListBox1.Int egralHeight = False
Me.ListBox1.Loc ation = New System.Drawing. Point(0, 0)
Me.ListBox1.Nam e = "ListBox1"
Me.ListBox1.Siz e = New System.Drawing. Size(284, 264)
Me.ListBox1.Tab Index = 0
'
'Form1
'
Me.AutoScaleDim ensions = New System.Drawing. SizeF(6.0!, 13.0!)
Me.AutoScaleMod e = System.Windows. Forms.AutoScale Mode.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
gmh = New GlobalMouseHook ()
End Sub
Private Sub Form1_FormClose d(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.FormClose dEventArgs) Handles MyBase.FormClos ed
gmh.Dispose()
End Sub
Private Sub gmh_MouseDown(B yVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles gmh.MouseDown
Me.ListBox1.Ite ms.Add(String.F ormat("{0} down at screen coordinate
({1},{2})", e.Button, e.X, e.Y))
End Sub
Private Sub gmh_MouseMove(B yVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles gmh.MouseMove
'Me.ListBox1.It ems.Add("Move")
End Sub
Private Sub gmh_MouseUp(ByV al sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles gmh.MouseUp
Me.ListBox1.Ite ms.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 MouseEventHandl er
Public Event MouseUp As MouseEventHandl er
Public Event MouseMove As MouseEventHandl er
Public Sub New()
Hook()
End Sub
Private Sub Hook()
Dim hInstance As IntPtr = LoadLibrary("Us er32")
hhk = SetWindowsHookE x(WH_MOUSE_LL, AddressOf Me.HookProc,
hInstance, 0)
End Sub
Private Sub Unhook()
UnhookWindowsHo okEx(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.Le ft, 0, lParam.pt.x, lParam.pt.y, 0))
Case WM_RBUTTONDOWN
RaiseEvent MouseDown(Me, New
MouseEventArgs( MouseButtons.Ri ght, 0, lParam.pt.x, lParam.pt.y, 0))
Case WM_MBUTTONDOWN
RaiseEvent MouseDown(Me, New
MouseEventArgs( MouseButtons.Mi ddle, 0, lParam.pt.x, lParam.pt.y, 0))
Case WM_LBUTTONUP, WM_RBUTTONUP, WM_MBUTTONUP
RaiseEvent MouseUp(Nothing , Nothing)
Case WM_MOUSEMOVE
RaiseEvent MouseMove(Nothi ng, Nothing)
Case WM_MOUSEWHEEL, WM_MOUSEHWHEEL
Case Else
Console.WriteLi ne(wParam)
End Select
End If
Return CallNextHookEx( hhk, nCode, wParam, lParam)
End Function
#Region "API Declarations"
<StructLayout(L ayoutKind.Seque ntial)_
Private Structure API_POINT
Public x As Integer
Public y As Integer
End Structure
<StructLayout(L ayoutKind.Seque ntial)_
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 LowLevelMouseHo okProc(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 SetWindowsHookE x Lib "user32.dll " (ByVal
idHook As Integer, ByVal lpfn As LowLevelMouseHo okProc, 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 UnhookWindowsHo okEx Lib "user32" (ByVal hhk As
IntPtr) As Boolean
#End Region
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValu e Then
If disposing Then
' TODO: free other state (managed objects).
End If
Unhook()
End If
Me.disposedValu e = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable
pattern.
Public Sub Dispose() Implements IDisposable.Dis pose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFina lize(Me)
End Sub
#End Region
End Class