mouse position + mousehook
-
Tuesday, September 05, 2006 9:53 AM
hi all,
I am trying to catch the mouse events all over the screen. I did a lot of researchs about that but unfortunatelly even the samples I found were in C and unfortunatelly I am using VB.NET 2005.
by saying that I managed to find one sample which works fine just on the current application's form.
but this is not what I want. I want to catch any mouse events that been made on the screen.
And I also want to turn on or off this facility. For example if I click button1 it will start and if I click button2 it will stop.
This following code is the one I managed to find. It is quite urgent and I am looking forward to see your helps.
Thanks
Imports
System.Runtime.InteropServicesPublic
Class Form1 Private Structure MOUSEHOOKSTRUCT Public pt As Point Public hWnd As IntPtr Public hitTest As Int32 Public extra As Int32 End Structure Private _mouseHook As Int32 = 0 Private Const WH_MOUSE As Int32 = 7 Private Delegate Function CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32<MarshalAs(UnmanagedType.FunctionPtr)>
Private _mouseProc As CallBack Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, ByVal hInstance As IntPtr, ByVal wParam As Int32) As Int32 Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32 Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If _mouseHook = 0 Then_mouseProc =
New CallBack(AddressOf MouseHookProc)_mouseHook = SetWindowsHookExW(WH_MOUSE, _mouseProc, IntPtr.Zero, GetCurrentThreadId)
Dim err As Integer = Marshal.GetLastWin32Error End If End Sub Private Shared Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As Int32Debug.Print(
"Message = {0}, x={1}, y={2}", wParam.ToInt32, lParam.pt.X, lParam.pt.Y)Form1.Text = lParam.pt.X
Return CallNextHookEx(WH_MOUSE, nCode, wParam, lParam) End Function Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click_mouseProc =
New CallBack(AddressOf MouseHookProc)_mouseHook = 0
End SubEnd
Class
All Replies
-
Tuesday, September 05, 2006 2:10 PMModerator
Hmm, looks familiar. I updated the code to install a low-level global mouse hook:
Imports System.Runtime.InteropServices
Public Class Form1
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
Private _mouseHook As IntPtr
Private Const WH_MOUSE_LL As Int32 = 14
Private Delegate Function CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
<MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As CallBack
Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, 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 Function InstallHook() As Boolean
If _mouseHook = IntPtr.Zero Then
_mouseProc = New CallBack(AddressOf MouseHookProc)
_mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
End If
Return _mouseHook <> IntPtr.Zero
End Function
Public Sub RemoveHook()
If _mouseHook = IntPtr.Zero Then Return
UnhookWindowsHookEx(_mouseHook)
_mouseHook = IntPtr.Zero
End Sub
Private Shared Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Debug.Print("Message = {0}, x={1}, y={2}", wParam.ToInt32, lParam.pt.X, lParam.pt.Y)
Form1.Text = CStr(lParam.pt.X)
Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InstallHook()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
RemoveHook()
End Sub
End Class
-
Tuesday, September 05, 2006 3:32 PM
hi nobugz
thank you ever so much. you are a lifesaver. thanks a lot.

-
Wednesday, January 30, 2008 3:36 PM
Hi nobugz,
I have some what different requirement.
I have two application(Win32 exe). One application contains a button on it & second application have many controls on it.
On click of button I need my mouse pointer should move to second application & highlight a control within it.
So how to move the mouse from one window to other window .
Any help appreciated
Yogesh
- Proposed As Answer by ShariqDON Tuesday, October 27, 2009 10:28 AM

