#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);
      //  }

C# 通过Hook的方法 屏蔽快捷键的更多相关文章

  1. C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入

    原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...

  2. {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm

    Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...

  3. idea快捷键:查找类中所有方法的快捷键

    查找类中所有方法的快捷键 第一种:ctal+f12,如下图 第二种:alt+7,如下图

  4. Android进程so注入Hook java方法

    本文博客链接:http://blog.csdn.net/qq1084283172/article/details/53769331 Andorid的Hook方式比较多,现在来学习下,基于Android ...

  5. idea 查看 类所有方法的快捷键

    idea 查看 类 所有方法的快捷键 Idea:ctrl+F12 Eclipse:Ctrl+O

  6. 【Eclipse】_Eclipse自动补全增强方法 & 常用快捷键

    一,Eclipse自动补全增强方法 在Eclipse中,从Window -> preferences -> Java -> Editor -> Content assist - ...

  7. Eclipse自动生成方法注释 快捷键

    自动生成方法的注释格式,例如 /*** @param str* @return* @throws ParseException*/ 快捷键是 ALT + SHIFT + J,将光标放在方法名上,按快捷 ...

  8. c# 屏蔽快捷键

    前言 有时候开发会遇到这样一个需求,软件需要屏蔽用户的组合快捷键或某些按键,避免强制退出软件,防止勿操作等. 原理 1.要实现组合键,按键拦截,需要用到user32.dll中的SetWindowsHo ...

  9. 【转】微软教学:三种方法屏蔽Win7/Win8.1升级Win10推送

    原文地址:http://www.ithome.com/html/win10/199961.htm 微软在2015年6月就开启了Win10升级推送工作,主要是靠<获取Windows10>工具 ...

随机推荐

  1. BZOJ3534 [Sdoi2014]重建 【矩阵树定理】

    题目 T国有N个城市,用若干双向道路连接.一对城市之间至多存在一条道路. 在一次洪水之后,一些道路受损无法通行.虽然已经有人开始调查道路的损毁情况,但直到现在几乎没有消息传回. 辛运的是,此前T国政府 ...

  2. spring中MessageSource的配置使用方法3--ResourceBundleMessageSource

    ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化).与HierarchicalMessageSource一起使用,它还能够处理 ...

  3. OpenJudge 东方14ACM小组 / 20170123 02 岛屿

    总时间限制:  40000ms 单个测试点时间限制:  4000ms 内存限制:  128000kB 描述 从前有一座岛屿,这座岛屿是一个长方形,被划为N*M的方格区域,每个区域都有一个确定的高度.不 ...

  4. 写了一个可以个性化设置的仿<select>标签

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Android上下文Context

    Android上下文Context介绍 在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiv ...

  6. 标准C程序设计七---44

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  7. Integration_Unit test coding standard

    Integration & Unit test coding standard 命名规则 好的命名规则,直接从命名就可以清楚的知道该测试方法测试的内容和目的,而不用额外的添加注释说明.对于MV ...

  8. Codeforces 629 A. Far Relative’s Birthday Cake

      A. Far Relative’s Birthday Cake   time limit per test 1 second memory limit per test 256 megabytes ...

  9. Presto查询引擎简单分析

    Hive查询流程分析 各个组件的作用 UI(user interface)(用户接口):提交数据操作的窗口Driver(引擎):负责接收数据操作,实现了会话句柄,并提供基于JDBC / ODBC的ex ...

  10. Chelly的串串专题

    CF149E 题意:给出一个长度为n的文本串和m个模式串,求有多少个模式串可以拆成两半,使得这两半按顺序匹配(n<=2e5,m<=100) 最暴力的想法就是对于每个询问串,全部和原串做一遍 ...