现在要做的一项目牵扯到这个问题,所以问下,谢谢了。有现成的源码最好。

解决方案 »

  1.   

    using System;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;
    using System.ComponentModel;namespace Cls
    {
        public class UserActivityHook
        {
            #region Windows structure definitions
            [StructLayout(LayoutKind.Sequential)]
            private class POINT
            {
                public int x;
                public int y;
            }
            [StructLayout(LayoutKind.Sequential)]
            private class MouseHookStruct
            {
                public POINT pt;
                public int hwnd;
                public int wHitTestCode;
                public int dwExtraInfo;
            }
            [StructLayout(LayoutKind.Sequential)]
            private class MouseLLHookStruct
            {
                public POINT pt;
                public int mouseData;
                public int flags;
                public int time;
                public int dwExtraInfo;
            }
            [StructLayout(LayoutKind.Sequential)]
            private class KeyboardHookStruct
            {
                public int vkCode;
                public int scanCode;
                public int flags;
                public int time;
                public int dwExtraInfo;
            }
            #endregion        #region Windows function imports
            [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "FindWindow")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("user32.dll ", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern bool EnableWindow(IntPtr hWnd, bool enable);
            [DllImport("user32.dll", CharSet = CharSet.Auto,
               CallingConvention = CallingConvention.StdCall, SetLastError = true)]
            public static extern int SetWindowsHookEx(
                int idHook,
                HookProc lpfn,
                IntPtr hMod,
                int dwThreadId);
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                CallingConvention = CallingConvention.StdCall, SetLastError = true)]
            public static extern int UnhookWindowsHookEx(int idHook);
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                 CallingConvention = CallingConvention.StdCall)]
            public static extern int CallNextHookEx(
                int idHook,
                int nCode,
                int wParam,
                IntPtr lParam);        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
            [DllImport("user32")]
            private static extern int ToAscii(
                int uVirtKey,
                int uScanCode,
                byte[] lpbKeyState,
                byte[] lpwTransKey,
                int fuState);        [DllImport("user32")]
            private static extern int GetKeyboardState(byte[] pbKeyState);        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            private static extern short GetKeyState(int vKey);
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID);        #endregion        #region Windows constants        private const int WH_MOUSE_LL       = 14;
            private const int WH_KEYBOARD_LL    = 13;
            private const int WH_MOUSE          = 7;
            private const int WH_KEYBOARD       = 2;
            private const int WM_MOUSEMOVE      = 0x200;
            private const int WM_LBUTTONDOWN    = 0x201;
            private const int WM_RBUTTONDOWN    = 0x204;
            private const int WM_MBUTTONDOWN    = 0x207;
            private const int WM_LBUTTONUP      = 0x202;
            private const int WM_RBUTTONUP      = 0x205;
            private const int WM_MBUTTONUP      = 0x208;
            private const int WM_LBUTTONDBLCLK  = 0x203;
            private const int WM_RBUTTONDBLCLK  = 0x206;
            private const int WM_MBUTTONDBLCLK  = 0x209;
            private const int WM_MOUSEWHEEL     = 0x020A;
            private const int WM_KEYDOWN = 0x100;
            private const int WM_KEYUP = 0x101;
            private const int WM_SYSKEYDOWN = 0x104;
            private const int WM_SYSKEYUP = 0x105;
            private const byte VK_SHIFT     = 0x10;
            private const byte VK_CAPITAL   = 0x14;
            private const byte VK_NUMLOCK   = 0x90;
            private const byte VK_TAB = 0x09;
            private const byte LLKHF_ALTDOWN = 0x20;
            private const byte VK_ESCAPE = 0x1B;
            private const byte VK_LCONTROL = 0xA2;
            private const byte VK_RCONTROL = 0xA3;
            private const byte VK_F4 = 0x73;
            
            #endregion
      

  2.   

            public UserActivityHook()
            {
                Start();
            }        public UserActivityHook(bool InstallMouseHook, bool InstallKeyboardHook)
            {
                Start(InstallMouseHook, InstallKeyboardHook);
            }        ~UserActivityHook()
            {
                //uninstall hooks and do not throw exceptions
                Stop(true, true, false);
            }
            public event MouseEventHandler OnMouseActivity;
            public event KeyEventHandler KeyDown;
            public event KeyPressEventHandler KeyPress;
            public event KeyEventHandler KeyUp;
            private int hMouseHook = 0;
            private int hKeyboardHook = 0;
            private static HookProc MouseHookProcedure;
            private static HookProc KeyboardHookProcedure;
            public void Start()
            {
                this.Start(true, true);
            }
            private void EnableTrayBar(bool b) 
            {
                IntPtr wnd = FindWindow("Shell_traywnd", null);
                EnableWindow(wnd, b);
            }
            public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
            {
                //disable shell tray bar
                EnableTrayBar(false);
                // install Mouse hook only if it is not installed and must be installed
                if (hMouseHook == 0 && InstallMouseHook)
                {
                    // Create an instance of HookProc.
                    MouseHookProcedure = new HookProc(MouseHookProc);
                    //install hook
                    hMouseHook = SetWindowsHookEx(
                        WH_MOUSE_LL,
                        MouseHookProcedure,
                        Marshal.GetHINSTANCE(
                            Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);
                    //If SetWindowsHookEx fails.
                    if (hMouseHook == 0)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //do cleanup
                        Stop(true, false, false);
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }            // install Keyboard hook only if it is not installed and must be installed
                if (hKeyboardHook == 0 && InstallKeyboardHook)
                {
                    // Create an instance of HookProc.
                    KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                    //install hook
                    hKeyboardHook = SetWindowsHookEx(
                        WH_KEYBOARD_LL,
                        KeyboardHookProcedure,
                        Marshal.GetHINSTANCE(
                        Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);
                    //If SetWindowsHookEx fails.
                    if (hKeyboardHook == 0)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //do cleanup
                        Stop(false, true, false);
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }
            }
            public void Stop()
            {
                this.Stop(true, true, true);
            }
      

  3.   

            public void Stop(bool UninstallMouseHook, bool UninstallKeyboardHook, bool ThrowExceptions)
            {
                //enable shell tray bar
                EnableTrayBar(true);
                //if mouse hook set and must be uninstalled
                if (hMouseHook != 0 && UninstallMouseHook)
                {
                    //uninstall hook
                    int retMouse = UnhookWindowsHookEx(hMouseHook);
                    //reset invalid handle
                    hMouseHook = 0;
                    //if failed and exception must be thrown
                    if (retMouse == 0 && ThrowExceptions)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }            //if keyboard hook set and must be uninstalled
                if (hKeyboardHook != 0 && UninstallKeyboardHook)
                {
                    //uninstall hook
                    int retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                    //reset invalid handle
                    hKeyboardHook = 0;
                    //if failed and exception must be thrown
                    if (retKeyboard == 0 && ThrowExceptions)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);
                    }
                }
            }
            private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
            {
                // if ok and someone listens to our events
                //bool Disable = false;
                if ((nCode >= 0) && (OnMouseActivity != null))
                {
                    //Marshall the data from callback.
                    MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));                //detect button clicked
                    MouseButtons button = MouseButtons.None;
                    short mouseDelta = 0;
                    switch (wParam)
                    {
                        case WM_LBUTTONDOWN:
                            //case WM_LBUTTONUP: 
                            //case WM_LBUTTONDBLCLK: 
                            button = MouseButtons.Left;
                            break;
                        case WM_RBUTTONDOWN:
                            //case WM_RBUTTONUP: 
                            //case WM_RBUTTONDBLCLK: 
                            button = MouseButtons.Right;
                            //Disable = true;
                            break;
                        case WM_MOUSEWHEEL:
                            mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);
                            break;
                    }                //double clicks
                    int clickCount = 0;
                    if (button != MouseButtons.None)
                        if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;
                        else clickCount = 1;                //generate event 
                    MouseEventArgs e = new MouseEventArgs(
                                                       button,
                                                       clickCount,
                                                       mouseHookStruct.pt.x,
                                                       mouseHookStruct.pt.y,
                                                       mouseDelta);
                    //raise it
                    OnMouseActivity(this, e);
                }
                //call next hook
                return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
            }
            private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
            {
                //indicates if any of underlaing events set e.Handled flag
                bool handled = false;
                //it was ok and someone listens to events
                if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
                {
                    //read structure KeyboardHookStruct at lParam
                    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                    if (((Keys)MyKeyboardHookStruct.vkCode == Keys.LWin) || ((Keys)MyKeyboardHookStruct.vkCode == Keys.RWin)||
                        ((MyKeyboardHookStruct.vkCode == VK_TAB) && ((MyKeyboardHookStruct.flags & LLKHF_ALTDOWN) != 0))||
                        ((MyKeyboardHookStruct.vkCode == VK_ESCAPE) && ((MyKeyboardHookStruct.flags & LLKHF_ALTDOWN) != 0))||
                        ((MyKeyboardHookStruct.vkCode == VK_F4) && ((MyKeyboardHookStruct.flags & LLKHF_ALTDOWN) != 0)) ||
                        (MyKeyboardHookStruct.vkCode == VK_ESCAPE) && ((GetKeyState(VK_LCONTROL) & 0x8000) != 0)||
                        (MyKeyboardHookStruct.vkCode == VK_ESCAPE) && ((GetKeyState(VK_RCONTROL) & 0x8000) != 0)
                        ) 
                    {
                        handled = true;
                    }
                }
                //if event handled in application do not handoff to other listeners
                if (handled)
                    return 1;
                else
                    return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
            }
        }
    }
      

  4.   

    以前做过这样的程序,新建一个cs文件将上面的3部分拷贝到里面,下面是调用的方法CTRL+ALT+DEL你可以在注册表里面进行控制,这个不但可以控制键盘还可以控制鼠标
            #region golbal hook
            Cls.UserActivityHook actHook;
            public void MouseMoved(object sender, MouseEventArgs e)
            {
               //--
            }
            public void MyKeyDown(object sender, KeyEventArgs e)
            {
                //LogWrite("KeyDown  - " + e.KeyData.ToString());
            }        public void MyKeyPress(object sender, KeyPressEventArgs e)
            {
                //LogWrite("KeyPress  - " + e.KeyChar);
            }        public void MyKeyUp(object sender, KeyEventArgs e)
            {
                //LogWrite("KeyUp  - " + e.KeyData.ToString());
            }
            #endregion        
            public FormMain()
            {
                InitializeComponent();
                actHook = new Cls.UserActivityHook();
                actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
                actHook.KeyDown += new KeyEventHandler(MyKeyDown);
                actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
                actHook.KeyUp += new KeyEventHandler(MyKeyUp);
                actHook.Start();
            }
      

  5.   

    private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
            {
                //indicates if any of underlaing events set e.Handled flag
                bool handled = false;
                //it was ok and someone listens to events
                if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
                {
                    //read structure KeyboardHookStruct at lParam
                    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                    if (((Keys)MyKeyboardHookStruct.vkCode == Keys.LWin) || ((Keys)MyKeyboardHookStruct.vkCode == Keys.RWin)||
                        ((MyKeyboardHookStruct.vkCode == VK_TAB) && ((MyKeyboardHookStruct.flags & LLKHF_ALTDOWN) != 0))||
                        ((MyKeyboardHookStruct.vkCode == VK_ESCAPE) && ((MyKeyboardHookStruct.flags & LLKHF_ALTDOWN) != 0))||
                        ((MyKeyboardHookStruct.vkCode == VK_F4) && ((MyKeyboardHookStruct.flags & LLKHF_ALTDOWN) != 0)) ||
                        (MyKeyboardHookStruct.vkCode == VK_ESCAPE) && ((GetKeyState(VK_LCONTROL) & 0x8000) != 0)||
                        (MyKeyboardHookStruct.vkCode == VK_ESCAPE) && ((GetKeyState(VK_RCONTROL) & 0x8000) != 0)
                        ) 
    //屏蔽键盘按键的关键代码
                    {
                        handled = true;
                    }
                }
                //if event handled in application do not handoff to other listeners
                if (handled)
                    return 1;
                else
                    return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
            }
      

  6.   

    Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr" 等于1,即可屏蔽CTRL+ALT+DEL
      

  7.   

    目前最好的屏蔽方法就是DirectInput 设置成独享键盘模式即可 否则CTRL ALT DEL屏蔽不了 这一招对所有版本的WINDOWS通吃 因为是硬件级别的屏蔽了
      

  8.   

    hongqi162 的代码好NB,直接把 任务栏也屏蔽了  任务栏上 点 左右鼠都没用
      

  9.   

    public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
            {
                //disable shell tray bar
                EnableTrayBar(false);
                // install Mouse hook only if it is not installed and must be installed
                if (hMouseHook == 0 && InstallMouseHook)
                {
                    // Create an instance of HookProc.
                    MouseHookProcedure = new HookProc(MouseHookProc);
                    //install hook
                    hMouseHook = SetWindowsHookEx(
                        WH_MOUSE_LL,
                        MouseHookProcedure,
                        Marshal.GetHINSTANCE(
                            Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);
                    //If SetWindowsHookEx fails.
                    if (hMouseHook == 0)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //do cleanup
                        Stop(true, false, false);
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);                }
                }
    执行时上面红色地方报错,提示为:未处理:Win32Exception
      

  10.   

    public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
            {
                //disable shell tray bar
                EnableTrayBar(false);
                // install Mouse hook only if it is not installed and must be installed
                if (hMouseHook == 0 && InstallMouseHook)
                {
                    // Create an instance of HookProc.
                    MouseHookProcedure = new HookProc(MouseHookProc);
                    //install hook
                    hMouseHook = SetWindowsHookEx(
                        WH_MOUSE_LL,
                        MouseHookProcedure,
                        Marshal.GetHINSTANCE(
                            Assembly.GetExecutingAssembly().GetModules()[0]),
                        0);
                    //If SetWindowsHookEx fails.
                    if (hMouseHook == 0)
                    {
                        //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
                        int errorCode = Marshal.GetLastWin32Error();
                        //do cleanup
                        Stop(true, false, false);
                        //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                        throw new Win32Exception(errorCode);                }
                }
    执行时上面红色地方报错,提示为:未处理:Win32Exception