在项目里经常要设计一个窗体全屏,然后屏蔽一些窗体的事件,比如 屏蔽开始按钮 alt+F4等等按钮,这里给出示例代码希望对大家有帮助
1 static int hHook = 0; //全局钩子数 2 3 public const int WH_KEYBOARD_LL = 13; //LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取 4 5 //委托 6 public delegate int HookProc(int nCode, int wParam, IntPtr lParam); 7 event HookProc KeyBoardHookProcedure; 8 9 //键盘Hook结构 10 [StructLayout(LayoutKind.Sequential)] 11 public class KeyBoardHookStruct 12 { 13 public int vkCode; 14 public int scanCode; 15 public int flags; 16 public int time; 17 public int dwExtraInfo; 18 } 19 20 #region DllImport 21 //设置钩子 22 [DllImport("user32.dll")] 23 public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); 24 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 25 //抽掉钩子 26 public static extern bool UnhookWindowsHookEx(int idHook); 27 [DllImport("user32.dll")] 28 //调用下一个钩子 29 public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); 30 31 [DllImport("kernel32.dll")] 32 public static extern int GetCurrentThreadId(); 33 34 [DllImport("kernel32.dll")] 35 public static extern IntPtr GetModuleHandle(string name); 36 37 #endregion 38 39 #region 自定义事件函数 40 41 public void Hook_Start() 42 { 43 // 安装键盘钩子 44 if (hHook == 0) 45 { 46 KeyBoardHookProcedure = new HookProc(KeyBoardHookProc); 47 48 hHook = SetWindowsHookEx(WH_KEYBOARD_LL,KeyBoardHookProcedure,GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); 49 50 //如果设置钩子失败. 51 if (hHook == 0) 52 { 53 Hook_Clear(); 54 //throw new Exception("设置Hook失败!"); 55 } 56 } 57 } 58 59 //取消钩子事件 60 public void Hook_Clear() 61 { 62 bool retKeyboard = true; 63 if (hHook != 0) 64 { 65 retKeyboard = UnhookWindowsHookEx(hHook); 66 hHook = 0; 67 } 68 //如果去掉钩子失败. 69 if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed."); 70 } 71 72 //这里添加自己想要的信息处理 73 public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam) 74 { 75 if (nCode >= 0) 76 { 77 KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct)); 78 if (kbh.vkCode == 91) // 截获左win(开始菜单键) 79 { 80 return 1; 81 } 82 if (kbh.vkCode == 92)// 截获右win 83 { 84 return 1; 85 } 86 //截获Ctrl+Esc 87 if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control) 88 { 89 return 1; 90 } 91 //截获alt+f4 92 if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt) 93 { 94 return 1; 95 } 96 //截获alt+tab 97 if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) 98 { 99 return 1; 100 } 101 //截获Ctrl+Shift+Esc 102 if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) 103 { 104 return 1; 105 } 106 //截获alt+空格 107 if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Alt) 108 { 109 return 1; 110 } 111 //截获Ctrl+Alt+Delete 112 if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete) 113 { 114 return 1; 115 } 116 117 118 } 119 return CallNextHookEx(hHook, nCode, wParam, lParam); 120 } 121 122 #endregion 123 124 125 private void frmMain_Load(object sender, EventArgs e) 126 { 127 Hook_Start(); 128 } 129 130 private void frmMain_FormClosing(object sender, FormClosingEventArgs e) 131 { 132 Hook_Clear(); 133 }
代码基本就是这样,如果不明白可以大家交流一下