偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C#實(shí)現(xiàn)全局鉤子步驟

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

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

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

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

首先倒入所需要的windows函數(shù),主要有三個(gè),SetWindowsHookEX用來安裝鉤子,UnhookWindowsHookEX用來卸載鉤子以及CallNextHookEX用來將hook信息傳遞到鏈表中下一個(gè)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); 

有關(guān)這兩個(gè)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

在安裝全局鉤子的時(shí)候,我們就要做替換了,將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#實(shí)現(xiàn)全局鉤子了,而且,不需要寫DLL。看一下程序運(yùn)行情況:

下面是關(guān)于鼠標(biāo)和鍵盤的兩個(gè)Callback函數(shù):

  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運(yùn)算符
  3. C# WiteOne學(xué)習(xí)筆記
  4. 用C# ListView顯示數(shù)據(jù)記錄
  5. C# ConfigDlg.cs源程序
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-09-03 14:49:49

C#實(shí)現(xiàn)網(wǎng)絡(luò)點(diǎn)對(duì)點(diǎn)

2009-08-13 17:15:44

C#屏幕保護(hù)程序

2009-08-25 17:13:57

C#串口編程

2009-08-24 13:04:44

操作步驟C#字符串

2009-08-11 15:46:15

C#日歷控件

2010-06-09 10:20:56

鏈接MySQL數(shù)據(jù)庫(kù)

2009-07-31 16:48:44

C#位運(yùn)算

2009-08-11 13:27:09

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

2009-09-24 15:10:54

C#調(diào)用COM組件

2009-08-19 17:00:07

C#實(shí)現(xiàn)PrintPa

2009-08-20 14:22:17

C#實(shí)現(xiàn) Contro

2009-08-25 17:55:52

C#實(shí)現(xiàn)Strateg

2009-08-31 15:55:17

C#實(shí)現(xiàn)Strateg

2009-09-01 18:29:10

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

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2024-12-02 08:30:00

2010-07-16 09:30:42

C#MongoDB

2009-07-17 11:42:06

C#實(shí)現(xiàn)Web代理服務(wù)

2009-08-04 12:56:51

C#自定義事件

2009-08-17 17:13:50

C#安裝包制作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)