自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#實現全局鉤子步驟

開發(fā) 后端
本文介紹C#實現全局鉤子,首先倒入所需要的windows函數,主要有三個,SetWindowsHookEX用來安裝鉤子,UnhookWindowsHookEX用來卸載鉤子。

怎樣在C#中使用全局鉤子?以前寫的全局鉤子都是用unmanaged C或C++寫個DLL來實現,可大家都知道,C#是基于.Net Framework的,是managed,怎么讓C#實現全局鉤子呢?于是開始到網上搜索,好不容易找到一篇,318804 - HOW TO: Set a Windows Hook in Visual C# .NET,里面詳細的說明了如何使用鼠標鉤子捕獲鼠標的移動等,可是,它只能在Application里起作用,出了Application就沒用了,就是說它還是沒有實現全局鉤子,而且文章結尾處說:“Global Hooks are not supported in the .NET Framework...”,這可怎么辦呢?

別擔心,辦法總是有的,經過一番摸索以后,發(fā)現WH_KEYBORAD_LL和WH_MOUSE_LL這兩個low-level的hook可以被安裝成全局的,這就好辦了,我們不妨用這兩個low-level的hook替換掉WH_KEYBORAD和WH_MOUSE,于是開始測試。結果成功了,在C#實現全局鉤子。

我們來看一下主要代碼段。

首先倒入所需要的windows函數,主要有三個,SetWindowsHookEX用來安裝鉤子,UnhookWindowsHookEX用來卸載鉤子以及CallNextHookEX用來將hook信息傳遞到鏈表中下一個hook處理過程。

  1. [DllImport(\"user32.dll\",CharSetCharSet=CharSet.Auto,  
  2. CallingConventionCallingConvention=CallingConvention.StdCall,SetLastError=true)]  
  3. privatestaticexternintSetWindowsHookEx(  
  4. intidHook,  
  5. HookProclpfn,  
  6. IntPtrhMod,  
  7. intdwThreadId);  
  8.  
  9. [DllImport(\"user32.dll\",CharSetCharSet=CharSet.Auto,  
  10. CallingConventionCallingConvention=CallingConvention.StdCall,SetLastError=true)]  
  11. privatestaticexternintUnhookWindowsHookEx(intidHook);  
  12.  
  13. [DllImport(\"user32.dll\",CharSetCharSet=CharSet.Auto,  
  14. CallingConventionCallingConvention=CallingConvention.StdCall)]  
  15. privatestaticexternintCallNextHookEx(  
  16. intidHook,  
  17. intnCode,  
  18. intwParam,  
  19. IntPtrlParam); 

有關這兩個low-level hook在Winuser.h中的定義

  1. <summary> 
  2. ///WindowsNT/2000/XP:
    Installsahookprocedurethatmonitorslow-levelmouseinputevents.  
  3. ///</summary>[Page]  
  4. privateconstintWH_MOUSE_LL=14;  
  5. /**////<summary> 
  6. ///WindowsNT/2000/XP:
    Installsahookprocedurethatmonitorslow-levelkeyboardinputevents.  
  7. ///</summary> 
  8. privateconstintWH_KEYBOARD_LL=13

在安裝全局鉤子的時候,我們就要做替換了,將WH_MOUSE和WH_KEYBORAD分別換成WH_MOUSE_LL和WH_KEYBORAD_LL:

  1. //installhook  
  2. hMouseHook=SetWindowsHookEx(  
  3. WH_MOUSE_LL, //原來是WH_MOUSE  
  4. MouseHookProcedure,  
  5. Marshal.GetHINSTANCE(  
  6. Assembly.GetExecutingAssembly().GetModules()[0]),  
  7. 0);  
  8.  
  9. //installhook  
  10. hKeyboardHook=SetWindowsHookEx(  
  11.  
  12. WH_KEYBOARD_LL,//原來是WH_KEYBORAD  
  13. KeyboardHookProcedure,  
  14. Marshal.GetHINSTANCE(  
  15. Assembly.GetExecutingAssembly().GetModules()[0]),  
  16. 0);[Page] 

這樣替換了之后,我們就可以C#實現全局鉤子了,而且,不需要寫DLL。看一下程序運行情況:

下面是關于鼠標和鍵盤的兩個Callback函數:

  1. private int MouseHookProc(int nCode, int wParam, IntPtr lParam)  
  2. {  
  3. // if ok and someone listens to our events  
  4. if ((nCode >= 0) && (OnMouseActivity != null))  
  5. {  
  6. //Marshall the data from callback.  
  7. MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.
    PtrToStructure(lParam, typeof(MouseLLHookStruct));  
  8.  
  9. //detect button clicked  
  10. MouseButtons button = MouseButtons.None;  
  11. short mouseDelta = 0;  
  12. switch (wParam)  
  13. {  
  14. case WM_LBUTTONDOWN:  
  15. //case WM_LBUTTONUP:   
  16. //case WM_LBUTTONDBLCLK:   
  17. button = MouseButtons.Left;  
  18. break; [Page]  
  19. case WM_RBUTTONDOWN:  
  20. //case WM_RBUTTONUP:   
  21. //case WM_RBUTTONDBLCLK:   
  22. button = MouseButtons.Right;  
  23. break;  
  24. case WM_MOUSEWHEEL:  
  25. //If the message is WM_MOUSEWHEEL, 
    the high-order word of mouseData member is the wheel delta.   
  26. //One wheel click is defined as WHEEL_DELTA, which is 120.   
  27. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value  
  28.  
  29. mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);  
  30. //TODO: X BUTTONS (I havent them so was unable to test)  
  31. //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, 
    WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,[Page]  
  32. //or WM_NCXBUTTONDBLCLK, 
    the high-order word specifies which X button was pressed or released,  
  33. //and the low-order word is reserved. 
    This value can be one or more of the following values.  
  34. //Otherwise, mouseData is not used.  
  35. break;  

【編輯推薦】

  1. C# Iterator迭代器模式
  2. 概述C# New運算符
  3. C# WiteOne學習筆記
  4. 用C# ListView顯示數據記錄
  5. C# ConfigDlg.cs源程序
責任編輯:佚名 來源: IT168
相關推薦

2009-09-03 14:49:49

C#實現網絡點對點

2009-08-25 17:13:57

C#串口編程

2009-08-13 17:15:44

C#屏幕保護程序

2009-08-11 15:46:15

C#日歷控件

2009-08-24 13:04:44

操作步驟C#字符串

2010-06-09 10:20:56

鏈接MySQL數據庫

2009-09-24 15:10:54

C#調用COM組件

2009-08-19 17:00:07

C#實現PrintPa

2009-08-20 14:22:17

C#實現 Contro

2009-08-25 17:55:52

C#實現Strateg

2009-07-31 16:48:44

C#位運算

2009-08-11 13:27:09

C#動態(tài)圖像按鈕

2009-08-31 15:55:17

C#實現Strateg

2009-08-26 09:54:45

C#打印預覽C#打印

2009-09-01 18:29:10

C#繼承C#多態(tài)

2024-12-02 08:30:00

2009-08-17 17:13:50

C#安裝包制作

2009-08-04 12:56:51

C#自定義事件

2009-07-17 11:42:06

C#實現Web代理服務

2010-07-16 09:30:42

C#MongoDB
點贊
收藏

51CTO技術棧公眾號