/// <summary> /// 安装Window钩子到主线程 /// </summary> /// <param name="hookType">钩子类型</param> /// <param name="callBack">回调函数</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <returns></returns> public static bool HookWindow(WinHookType hookType, WinHookCallback callBack) { if (callBack == null) { throw new ArgumentNullException("callBack"); } if (Enum.IsDefined(typeof(WinHookType), hookType) == false) { throw new ArgumentException("hookType的值无效"); } var callBackPointer = Marshal.GetFunctionPointerForDelegate(callBack); var state = Extern.HookWindow((int)hookType, callBackPointer); if (state == true) { ReferenceTable.AddRef(callBack); } return(state); }
private static extern int SetWindowsHookEx(WinHookType idHook, WinHookProcedure lpfn, IntPtr hMod, int dwThreadId);
/// <summary> /// 反安装Window钩子 /// </summary> /// <param name="hookType">钩子类型</param> public static void UnHookWindow(WinHookType hookType) { Extern.UnHookWindow((int)hookType); }
internal static extern int CallNextHookEx(WinHookType idHook, int nCode, IntPtr wParam, IntPtr lParam);