C# 通过Hook的方法 屏蔽快捷键

 #region 屏蔽Windows功能键(快捷键)
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        private static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;
 
        //LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,会在你截取之前获得键盘。 
        private static HookProc KeyBoardHookProcedure;
        //键盘Hook结构函数 
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
 
        //设置钩子 
        [DllImport("user32.dll")]
        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")]
        //调用下一个钩子 
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
 
        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();
 [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);
 
         //如果函数执行成功,返回值不为0。
         //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
         [DllImport("user32.dll", SetLastError = true)]
         public static extern bool RegisterHotKey(
             IntPtr hWnd,                //要定义热键的窗口的句柄
             int id,                     //定义热键ID(不能与其它ID重复)           
             int fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
             Keys vk                     //定义热键的内容
             );
 
         [DllImport("user32.dll", SetLastError = true)]
         public static extern bool UnregisterHotKey(
             IntPtr hWnd,                //要取消热键的窗口的句柄 
             int id                      //要取消热键的ID
             );
 
        public static void Hook_Start()
        {
            // 安装键盘钩子 
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
                hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure,
                        GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                //如果设置钩子失败. 
                if (hHook == 0)
                {
                    Hook_Clear();
                }
            }
        }
 
        //取消钩子事件 
        public static void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            //如果去掉钩子失败. 
            if (!retKeyboard) throw new Exception("Hook去除失败");
        }
 
//这里可以添加自己想要的信息处理 
        private static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
 
                if (kbh.vkCode == 91) // 截获左win(开始菜单键) 
                {
                    return 1;
                }
 
                if (kbh.vkCode == 92)// 截获右win(开始菜单键) 
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control) //截获Ctrl+Esc 
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt) //截获Alt+Esc 
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt) //截获alt+f4 
                {
                    return 1;
                }
  if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) //截获alt+tab
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) //截获Ctrl+Shift+Esc
                {
                    return 1;
                }
              
                if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Alt) //截获alt+空格 
                {
                    return 1;
                }
 
                if (kbh.vkCode == 241) //截获F1 
                {
                    return 1;
                }
 
                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)      //截获Ctrl+Alt+Delete 
                {
                    return 1;
                }
 
                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) //截获Ctrl+Shift 
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt) //截获Ctrl+Alt+空格 
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.LWin)
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.RWin)
                {
 
                } return 1;
     }
 
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
 
        /// <summary>
        /// 开打任务管理器快捷键为Windows底层按键
        /// </summary>
        /// <param name="bLock"></param>
        public static void TaskMgrLocking(bool bLock)
        {
            if (bLock)//屏蔽任务管理器、并且不出现windows提示信息“任务管理器已被管理员禁用”
            {
                Process p = new Process();
                p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
                p.StartInfo.FileName = "taskmgr.exe";
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.Start(); 
            }
            else//设置任务管理器为可启动状态 
            {
                Process[] p = Process.GetProcesses();
 
                foreach (Process p1 in p)
                {
                    try
                    {
                        if (p1.ProcessName.ToLower().Trim() == "taskmgr")//这里判断是任务管理器    
                        {
                            p1.Kill();
                            RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
                            r.SetValue("DisableTaskmgr", "0"); //设置任务管理器为可启动状态 
                            Registry.CurrentUser.DeleteSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System");
                              }
                    }
                    catch
                    {
                        return;
                    }
                }
                
            }
            
        }
        #endregion
 
        //调用
        private void button1_Click(object sender, EventArgs e)
        {
            //启动钩子,处理钩子事件
            Hook_Start();
            //屏蔽任务管理器
            TaskMgrLocking(true);
        }
        /// <summary>
        /// 关闭窗口时事件
        /// </summary>
        //private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        //{   //注销Id号为100的热键设定   
        //    UnregisterHotKey(Handle, 100);   //注销Id号为101的热键设定  
        //    UnregisterHotKey(Handle, 101);   //注销Id号为102的热键设定   
        //    UnregisterHotKey(Handle, 102);   //注销Id号为103的热键设定   
        //    UnregisterHotKey(Handle, 103);
        //    Hook_Clear();
        //    TaskMgrLocking(false);
      //  }
原文地址:https://www.cnblogs.com/liujiaknowledge/p/5035966.html