先來一個輔助類
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace HZCX.Utils { /// <summary> /// 鼠標全局鈎子 /// </summary> public static class MouseHook { private const int WM_MOUSEMOVE = 0x200; private const int WM_LBUTTONDOWN = 0x201; private const int WM_RBUTTONDOWN = 0x204; private const int WM_MBUTTONDOWN = 0x207; private const int WM_LBUTTONUP = 0x202; private const int WM_RBUTTONUP = 0x205; private const int WM_MBUTTONUP = 0x208; private const int WM_LBUTTONDBLCLK = 0x203; private const int WM_RBUTTONDBLCLK = 0x206; private const int WM_MBUTTONDBLCLK = 0x209; /// <summary> /// 點 /// </summary> [StructLayout(LayoutKind.Sequential)] public class POINT { public int x; public int y; } /// <summary> /// 鈎子結構體 /// </summary> [StructLayout(LayoutKind.Sequential)] public class MouseHookStruct { public POINT pt; public int hWnd; public int wHitTestCode; public int dwExtraInfo; } public const int WH_MOUSE_LL = 14; // mouse hook constant // 裝置鈎子的函數 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); // 卸下鈎子的函數 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); // 下一個鈎掛的函數 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); // 全局的鼠標事件 public static event MouseEventHandler OnMouseActivity; // 鈎子回調函數 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); // 聲明鼠標鈎子事件類型 private static HookProc _mouseHookProcedure; private static int _hMouseHook = 0; // 鼠標鈎子句柄 /// <summary> /// 啟動全局鈎子 /// </summary> public static void Start() { // 安裝鼠標鈎子 if (_hMouseHook != 0) { Stop(); } // 生成一個HookProc的實例. _mouseHookProcedure = new HookProc(MouseHookProc); _hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, _mouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetEntryAssembly().GetModules()[0]), 0); //假設裝置失敗停止鈎子 if (_hMouseHook == 0) { Stop(); throw new Exception("SetWindowsHookEx failed."); } } /// <summary> /// 停止全局鈎子 /// </summary> public static void Stop() { bool retMouse = true; if (_hMouseHook != 0) { retMouse = UnhookWindowsHookEx(_hMouseHook); _hMouseHook = 0; } // 假設卸下鈎子失敗 if (!(retMouse)) throw new Exception("UnhookWindowsHookEx failed."); } /// <summary> /// 鼠標鈎子回調函數 /// </summary> private static int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam) { // 假設正常執行而且用戶要監聽鼠標的消息 if ((nCode >= 0) && (OnMouseActivity != null)) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch (wParam) { case WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; case WM_LBUTTONUP: button = MouseButtons.Left; clickCount = 1; break; case WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case WM_RBUTTONDOWN: button = MouseButtons.Right; clickCount = 1; break; case WM_RBUTTONUP: button = MouseButtons.Right; clickCount = 1; break; case WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; } if (button != MouseButtons.None && clickCount>0) { // 從回調函數中得到鼠標的信息 MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct)); MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0); OnMouseActivity(null, e); } } // 啟動下一次鈎子 int inext = CallNextHookEx(_hMouseHook, nCode, wParam, lParam); return inext; } } }
Program的main函數里面調用 MouseHook.Start();
main函數里面寫 Application.ApplicationExit += Application_ApplicationExit;
static void Application_ApplicationExit(object sender, EventArgs e)
{
MouseHook.Stop();
}
窗體load里面寫 HZCX.Utils.MouseHook.OnMouseActivity += hook_OnMouseActivity;
窗體closing里面寫 HZCX.Utils.MouseHook.OnMouseActivity -= hook_OnMouseActivity;
void hook_OnMouseActivity(object sender, MouseEventArgs e) { try { if (this._isLoseFocusClose && e.Clicks > 0) { if (e.Button == System.Windows.Forms.MouseButtons.Left || e.Button == System.Windows.Forms.MouseButtons.Right) { if (!this.IsDisposed) { if (!this.ClientRectangle.Contains(this.PointToClient(e.Location))) { base.Close(); } } } } } catch { } }
就這樣了