123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using PTMedicalInsurance.Variables;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PTMedicalInsurance.Common.Hook
- {
- public class HookManager
- {
- #region 钩子类型的枚举
- public const int WH_JOURNALRECORD = 0;
- public const int WH_JOURNALPLAYBACK = 1;
- public const int WH_KEYBOARD = 2;
- public const int WH_GETMESSAGE = 3;
- public const int WH_CALLWNDPROC = 4;
- public const int WH_CBT = 5;
- public const int WH_SYSMSGFILTER = 6;
- public const int WH_MOUSE = 7;
- public const int WH_HARDWARE = 8;
- public const int WH_DEBUG = 9;
- public const int WH_SHELL = 10;
- public const int WH_FOREGROUNDIDLE = 11;
- public const int WH_CALLWNDPROCRET = 12;
- public const int WH_KEYBOARD_LL = 13;
- public const int WH_MOUSE_LL = 14;
- public const int WM_MOUSEMOVE = 0x200;
- public const int WM_LBUTTONDOWN = 0x201;
- public const int WM_RBUTTONDOWN = 0x204;
- public const int WM_MBUTTONDOWN = 0x207;
- public const int WM_LBUTTONUP = 0x202;
- public const int WM_RBUTTONUP = 0x205;
- public const int WM_MBUTTONUP = 0x208;
- public const int WM_LBUTTONDBLCLK = 0x203;
- public const int WM_RBUTTONDBLCLK = 0x206;
- public const int WM_MBUTTONDBLCLK = 0x209;
- public const int WM_KEYDOWN = 256;
- #endregion
- public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
-
- [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);
-
- [DllImport("kernel32.dll")]
- static extern int GetCurrentThreadId();
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern uint GetLastError();
- [DllImport("kernel32.dll")]
- static extern IntPtr LoadLibrary(string lpFileName);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern IntPtr GetModuleHandle(string lpModuleName);
- static int hMouseHook = 0;
- HookProc MouseHookProcedure;
- static int hKeyboardHook = 0;
- HookProc KeyboardHookProcedure;
- static int hWindowHook = 0;
- HookProc WindowHookProcedure;
- private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == WM_KEYDOWN)
- {
- int vkCode = Marshal.ReadInt32(lParam);
- if (vkCode.ToString() == "13")
- {
- MessageBox.Show("按了Enter");
- }
-
- return 1;
- }
- return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
- }
- private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
- {
- if (nCode >= 0)
- {
- switch (wParam)
- {
- case WM_LBUTTONDOWN:
- MessageBox.Show("鼠标左键按下");
- break;
- case WM_LBUTTONUP:
- MessageBox.Show("鼠标左键抬起");
- break;
- case WM_LBUTTONDBLCLK:
- MessageBox.Show("鼠标左键双击");
- break;
- case WM_RBUTTONDOWN:
- MessageBox.Show("鼠标右键按下");
- break;
- case WM_RBUTTONUP:
- MessageBox.Show("鼠标右键抬起");
- break;
- case WM_RBUTTONDBLCLK:
- MessageBox.Show("鼠标右键双击");
- break;
- }
- }
- return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
- }
- private int WindowHookProc(int nCode, Int32 wParam, IntPtr lParam)
- {
- if (nCode >= 0)
- {
- switch (wParam)
- {
- case WH_CALLWNDPROC:
- Global.writeLog("窗体消息过程");
- break;
- case WH_CBT:
- Global.writeLog("窗体创建");
- break;
- default:
- Global.writeLog("窗体创建1");
-
- break;
- }
- }
- return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
- }
-
- public void HookStart(IntPtr hInstance,int threadId)
- {
-
- if (hWindowHook == 0)
- {
-
- WindowHookProcedure = new HookProc(WindowHookProc);
-
- hWindowHook = SetWindowsHookEx(WH_CBT, WindowHookProcedure, hInstance, threadId);
-
- if (hWindowHook == 0)
- {
- uint lastError = GetLastError();
- HookStop();
- MessageBox.Show($"SetWindowsHookEx failed.Error code: {lastError}");
-
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- public void HookStop()
- {
- bool retKeyboard = true;
- bool retMouse = true;
- if (hKeyboardHook != 0)
- {
- retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
- hKeyboardHook = 0;
- }
- if (hMouseHook != 0)
- {
- retMouse = UnhookWindowsHookEx(hMouseHook);
- hMouseHook = 0;
- }
- if (hWindowHook != 0)
- {
- retMouse = UnhookWindowsHookEx(hWindowHook);
- hWindowHook = 0;
- }
- if (!(retMouse && retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");
- }
- }
- }
|