包括系統鉤子和線程鉤子,或者叫全域鉤子和私人鉤子。系統鉤子需要一個單獨的DLL,這個地方耽誤了不少時間,網上有說可以不用單獨DLL的。
現在開始貼代碼,代碼參照了紅黑聯盟中的一篇文章。
複製代碼 代碼如下:public class SetHook
{
public class HookTypes
{
/// <summary>
/// 鉤子類型
/// </summary>
public enum HookType
{
WH_JOURNALRECORD = 0,//對寄送至訊息佇列的輸入訊息進行記錄
WH_GETMESSAGE = 3,//對寄送至訊息佇列的訊息進行監視
WH_JOURNALPLAYBACK = 1,//對此前由WH_JOURNALRECORD 掛鈎處理過程紀錄的訊息進行寄送
WH_CALLWNDPROC = 4,//在系統將訊息發送至目標視窗處理過程之前,對該訊息進行監視
WH_CBT = 5,//接受對CBT應用程式有用的訊息
WH_MSGFILTER = -1,//監視由對話方塊、訊息框、菜單條、或捲軸中的輸入事件引發的訊息
WH_SYSMSGFILTER = 6,//監視由對話方塊,訊息框,菜單條,捲軸中的輸入事件引發的訊息
//8
WH_DEBUG = 9,//對其他鉤子處理過程進行調試
WH_SHELL = 10,//接受對外殼應用程式有用的通知
WH_FOREGROUNDIDLE = 11,//當應用程式的前台線程即將進入空閑狀態時被調用,它有助於在空閑時間內執行低優先順序的任務
#region 滑鼠和鍵盤事件
/// <summary>
/// 對擊鍵訊息進行監視
/// </summary>
WH_KEYBOARD = 2,
/// <summary>
/// 只能在windows NT中被安裝,用來對底層的鍵盤輸入事件進行監視
/// </summary>
WH_KEYBOARD_LL = 13,
/// <summary>
/// 對滑鼠訊息進行監視
/// </summary>
WH_MOUSE = 7,
/// <summary>
/// 只能在windows NT中被安裝,用來對底層滑鼠事件進行監視
/// </summary>
WH_MOUSE_LL = 14,
#endregion
}
}
public abstract class Hooks : HookTypes
{
public delegate int HookProc(int nCode, int wParam, IntPtr IParam);
/// <summary>
/// 安裝鉤子
/// </summary>
/// <param name="idHook">鉤子類型,即它處理的訊息類型</param>
/// <param name="lpfn"> 鉤子函數地址</param>
/// <param name="hInstance">應用程式執行個體的控制代碼。標識包含lpfn所指的子程的DLL</param>
/// <param name="threadId">安裝鉤子後想監控的線程的ID號, 如果為0,鉤子子程與所有的線程關聯,即為全域鉤子</param>
/// <returns>返回參數為鉤子控制代碼,失敗為NULL</returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(HookType hooktype, HookProc lpfn, IntPtr hInstance, int threadId);
/// <summary>
/// 卸載鉤子
/// </summary>
/// <param name="idHook">要卸載的鉤子控制代碼</param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
/// <summary>
/// 繼續下一個鉤子
/// </summary>
/// <param name="idHook"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="IParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr IParam);
/// <summary>
/// 擷取當前線程編號
/// </summary>
/// <returns></returns>
[DllImport("kernel32")]
public static extern int GetCurrentThreadId();
#region
/// <summary>
/// 委派物件
/// </summary>
public HookProc proc;
public abstract int SetWindowsHookEx();
#endregion
}
public class AddHook : Hooks
{
HookType hooktyp;
HookProc hookproc;
public AddHook(HookType _hooktype, HookProc _hookproc)
{
this.hooktyp = _hooktype;
this.hookproc = _hookproc;
}
public int AddPrivateHook()
{
return SetWindowsHookEx();
}
/// <summary>
/// 線程鉤子
/// </summary>
/// <returns></returns>
public override int SetWindowsHookEx()
{
//int theadId = System.Threading.Thread.CurrentThread.ManagedThreadId(); 應對GetCurrentThreadId()的過時
int hookId = 0;
object hookId_ = SetWindowsHookEx(this.hooktyp, this.hookproc, IntPtr.Zero, GetCurrentThreadId());
if (hookId_ != null)
{
hookId = (int)hookId_;
}
return hookId;
}
//系統鉤子和這差不多,安裝鉤子的時候後兩個參數不一樣
}
}
調用我建立了一個類,方便調用線程或者系統鉤子,這裡就不貼了