C#使用鼠标钩子
2015-03-23 22:23
330 查看
FormMain.cs
ClassWinAPI.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; // using HookTest; namespace HookTest { public partial class FormMain : Form { //钩子类型 /// WH_MOUSE_LL -> 14 public const int WH_MOUSE_LL = 14; // //Windows消息 /// WM_LBUTTONUP -> 0x0202 public const int WM_LBUTTONUP = 514; ClassWinAPI.HOOKPROC hHockProc;//钩子处理函数 int hMouseHook;//钩子句柄 public FormMain() { InitializeComponent(); } public IntPtr MouseHookProc(int code, int wParam, System.IntPtr lParam) { //Windows消息 uint msg = (uint)wParam; //鼠标信息 ClassWinAPI.MouseHookStruct mouseHookStruct = (ClassWinAPI.MouseHookStruct) System.Runtime.InteropServices.Marshal.PtrToStructure( lParam,typeof(ClassWinAPI.MouseHookStruct)); if (msg == WM_LBUTTONUP) { this.Text = mouseHookStruct.pt.x.ToString() + "--" + mouseHookStruct.pt.y.ToString(); } // IntPtr pMouseHook = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(hMouseHook)); System.Runtime.InteropServices.Marshal.StructureToPtr(hMouseHook, pMouseHook, true); System.Runtime.InteropServices.Marshal.DestroyStructure(pMouseHook,typeof(IntPtr)); return ClassWinAPI.CallNextHookEx(pMouseHook,code,wParam,lParam); } private void FormMain_Load(object sender, EventArgs e) { hHockProc = new ClassWinAPI.HOOKPROC(MouseHookProc); hMouseHook = ClassWinAPI.SetWindowsHookExW(WH_MOUSE_LL, hHockProc, IntPtr.Zero,0); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { ClassWinAPI.UnhookWindowsHookEx(hMouseHook); } } }
ClassWinAPI.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HookTest { class ClassWinAPI { [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)] public delegate IntPtr HOOKPROC(int code, int wParam, System.IntPtr lParam); [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct POINT { public int x; public int y; }; [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct MouseHookStruct { public POINT pt; public int hwnd; public int wHitTestCode; public int dwExtraInfo; }; [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetWindowsHookExW")] public static extern int SetWindowsHookExW( int idHook, HOOKPROC lpfn, [System.Runtime.InteropServices.InAttribute()] System.IntPtr hmod, uint dwThreadId); [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "UnhookWindowsHookEx")] [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] public static extern bool UnhookWindowsHookEx(int hhk); [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "CallNextHookEx")] [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.SysInt)] public static extern IntPtr CallNextHookEx( [System.Runtime.InteropServices.InAttribute()] System.IntPtr hhk, int nCode, int wParam, [System.Runtime.InteropServices.InAttribute()] System.IntPtr lParam); } }
相关文章推荐
- 使用C#钩子监视全局鼠标位置
- c#使用钩子拦截鼠标键盘事件
- C#中使用Hook(钩子)
- 如何在C#中使用全局鼠标、键盘Hook
- c#使用API进行模拟鼠标点击 底层操作同样简单
- c#中通过设置钩子监视鼠标移动
- c#中通过设置钩子监视鼠标移动
- C#中键盘钩子的使用
- C#中全局钩子函数使用示例
- 使用钩子函数[3] - 建立一个全局的鼠标钩子
- C#钩子实现简单鼠标键盘的监控和屏蔽
- C#中键盘钩子的使用(转)
- 如何在C#中使用全局鼠标、键盘Hook
- C#利用钩子控制鼠标【月儿原创】
- C#利用钩子控制鼠标【月儿原创】
- C#强化系列文章二:在C#中使用钩子
- 如何在C#中使用全局鼠标、键盘Hook
- 如何在C#中使用全局鼠标、键盘Hook
- C#中使用消息钩子的方法
- [转]C#中使用消息钩子的方法