遮蔽系統熱鍵鉤子Hook程式

yuejin發表於2014-01-06

在winform時候,經常需要做遮蔽系統熱鍵:

1、遮蔽左"WIN"、右"Win"

2、遮蔽Ctrl+Esc

3、遮蔽Alt+f4

4、遮蔽Alt+Esc

5、遮蔽Alt+Tab

6、截獲Ctrl+Shift+Esc

7、截獲Ctrl+Alt+Delete

......

public class Hook : IDisposable
    {
        //委託 
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;

        //LowLevel鍵盤截獲,如果是WH_KEYBOARD=2,並不能對系統鍵盤擷取,Acrobat Reader會在你擷取之前獲得鍵盤。 
        HookProc KeyBoardHookProcedure;

        //鍵盤Hook結構函式 
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        #region [DllImport("user32.dll")]
        //設定鉤子 
        [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 IntPtr GetModuleHandle(string name);
        #endregion

        #region 安裝鍵盤鉤子
        /// <summary>
        /// 安裝鍵盤鉤子
        /// </summary>
        public 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();
            }
        }
        #endregion

        #region 取消鉤子事件
        /// <summary>
        /// 取消鉤子事件
        /// </summary>
        public void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
        }
        #endregion


        #region 遮蔽鍵盤
        /// <summary>
        /// 遮蔽鍵盤
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
                // 遮蔽左"WIN"、右"Win"
                if ((kbh.vkCode == (int)Keys.LWin) || (kbh.vkCode == (int)Keys.RWin))
                    return 1;
                //遮蔽Ctrl+Esc
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)
                    return 1;
                //遮蔽Alt+f4 
                if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)
                    return 1;
                //遮蔽Alt+Esc
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt)
                    return 1;
                //遮蔽Alt+Tab 
                if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt)
                    return 1;
                //截獲Ctrl+Shift+Esc 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift)
                    return 1;
                //截獲Ctrl+Alt+Delete 
                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)
                    return 1;
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        #endregion

        #region 是否遮蔽CTRL+ALT+DEL             
        /// <summary>
        /// 是否遮蔽CTRL+ALT+DEL
        /// </summary>
        /// <param name="i">1=遮蔽 0=取消遮蔽</param>
        public static void ShieldMissionTask(int i)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser;
                RegistryKey key1 = key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
                key1.SetValue("DisableTaskMgr", i, Microsoft.Win32.RegistryValueKind.DWord);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        /// <summary>
        /// 釋放資源
        /// </summary>
        public void Dispose()
        {
            Hook_Clear();
        }
    }

 

需要用到的名稱空間

using System;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

 

說明:如果遮蔽CTRL+ALT+DEL時,文中方法是操作登錄檔,需要系統許可權,如果沒有系統許可權,會報錯

相關文章