第一步:将窗体的FormBorderStyle设置为none,WindowState设为Maximized

占据整个屏幕。

第二步:使用钩子监控全局键盘事件。即屏蔽掉大部分系统热键。但是屏蔽ctrl+alt+del 任务管理器则较复杂,这个特例后面讨论。

使用全局钩子应该注意的地方:将代码放到一个独立的类库里面(只有dll才能被注射到其他进程中)。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace KeyboardHookDLL
{
    public class KeyboardHook
    {
        public delegate int KeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        static int hKeyboardHook = 0;
        KeyboardProc KeyboardHookProcedure;

        /// <summary>
        /// 钩子函数,需要引用空间(using System.Reflection;)
        /// 线程钩子监听键盘消息设为2,全局钩子监听键盘消息设为13
        /// 线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14
        /// </summary>

        public const int WH_KEYBOARD = 13;
        public const int WH_MOUSE_LL = 14;

        public struct KeyboardMSG
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;

        }
        //private FileStream MyFs;
//各种键位的ASC码
        private const byte LLKHF_ALTDOWN = 0x20;
        private const byte VK_CAPITAL = 0x14;
        private const byte VK_ESCAPE = 0x1B;
        private const byte VK_F4 = 0x73;
        private const byte VK_LCONTROL = 0xA2;
        private const byte VK_NUMLOCK = 0x90;
        private const byte VK_RCONTROL = 0xA3;
        private const byte VK_SHIFT = 0x10;
        private const byte VK_TAB = 0x09;
        //public const int WH_KEYBOARD = 13;
        private const int WH_KEYBOARD_LL = 13;
        private const int WH_MOUSE = 7;
        //private const int WH_MOUSE_LL = 14;
        private const int WM_KEYDOWN = 0x100;
        private const int WM_KEYUP = 0x101;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_MBUTTONDBLCLK = 0x209;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_MBUTTONUP = 0x208;
        private const int WM_MOUSEMOVE = 0x200;
        private const int WM_MOUSEWHEEL = 0x020A;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_RBUTTONUP = 0x205;
        private const int WM_SYSKEYDOWN = 0x104;
        private const int WM_SYSKEYUP = 0x105;
        //private static int hKeyboardHook = 0;

        /// <summary>
        /// vs2008中的声明方法,在vs2010中略有不同

        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern int GetCurrentThreadId();

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =

CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, KeyboardProc lpfn, IntPtr

hInstance, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =

CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        public static extern short GetKeyState(int keycode);

//在这里你可以自己定义要拦截的键。
        private int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            KeyboardMSG m = (KeyboardMSG)Marshal.PtrToStructure(lParam, typeof(KeyboardMSG));
             if (
            ((int)m.vkCode == 91) || ((int)m.vkCode == 92) ||
            //两个组合键
            ((m.vkCode == VK_TAB) && ((m.flags & LLKHF_ALTDOWN) != 0)) ||

            ((m.vkCode == VK_ESCAPE) && ((m.flags & LLKHF_ALTDOWN) != 0)) ||
            ((m.vkCode == VK_F4) && ((m.flags & LLKHF_ALTDOWN) != 0)) ||
            //用于三个组合键
            (m.vkCode == VK_ESCAPE) && ((GetKeyState(VK_LCONTROL) & 0x8000) != 0) ||
            ((int)m.vkCode >= 65 && (int)m.vkCode <= 90 && ((GetKeyState(0x12) & 0x8000) != 0) ) ||
            ((int)m.vkCode >= 65 && (int)m.vkCode <= 90&& ((GetKeyState(0x11) & 0x8000) != 0)) ||
            (m.vkCode == VK_ESCAPE) && ((GetKeyState(VK_RCONTROL) & 0x8000) != 0)
            )
            {
                return 1;
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }
   // 安装钩子
        public void KeyMaskStart()
        {
            if (hKeyboardHook == 0)
            {
                // 创建HookProc实例
                KeyboardHookProcedure = new KeyboardProc(KeyboardHookProc);

                // 设置线程钩子
                hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProcedure,
                    Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                // 如果设置钩子失败
                if (hKeyboardHook == 0)
                {
                    KeyMaskStop();
                    throw new Exception("SetWindowsHookEx failed.");
                }
                ////用二进制流的方法打开任务管理器。而且不关闭流.这样任务管理器就打开不了
                //MyFs = new FileStream(Environment.ExpandEnvironmentVariables("%windir%\\system32\\taskmgr.exe"),
                //FileMode.Open);
                //byte[] MyByte = new byte[(int)MyFs.Length];
                //MyFs.Write(MyByte, 0, (int)MyFs.Length);
            }
        }

        // 卸载钩子
        public void KeyMaskStop()
        {
            bool retKeyboard = true;
            if (hKeyboardHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = 0;
            }
            if (!(retKeyboard))
            {
                throw new Exception("UnhookWindowsHookEx  failed.");
            }
        }
    }
}

第三步:这个时候你会发现,程序能屏蔽大部分的热键了,唯独ctrl+alt+del,你无论怎么去拦截,只要你按下这三个键,任务管理器活灵活现的出来了。

原因:这个组合键是系统级别滴(唯一的,ctrl+shift+esc不是)。普通的软件拦截是木有用的。得驱动级啥的才行吧。

既然如此,就只能转换思路了。既然不能治本,治标也行,只要能达到目的。

 

思路有两个:1是让任务管理器一旦运行就被结束,让用户不能操作。2是让任务管理器以另外的方式先运行并隐藏起来,用户就不能运行了。

 

思路1的代码:使用timer控件

//引用这个空间using System.Diagnostics;
        private void timer1_Tick(object sender, EventArgs e)
        {
            Process[] p = Process.GetProcesses();

            foreach (Process p1 in p)
            {
                try
                {
                    if (p1.ProcessName.ToLower().Trim() == "taskmgr")//这里判断是任务管理器   
                    {
                        p1.Kill();
                        return;
                    }
                }
                catch
                {
                    return;
                }
            }
        }

思路2有两个比较好的办法

还有个不咋滴的办法,就是以文件流的方式打开taskmgr.exe(代码在上面的KeyboardHookDLL中能找到),在xp下还行,到win7下面就会有提示了。

办法1:以隐藏的方式运行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace KidWorld
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll")]

        public static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll")]

        public static extern Int32 SendMessage(

        int hWnd, // handle to destination window

        int Msg, // message

        int wParam, // first message parameter

        int lParam); // second message parameter
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

            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();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            const int WM_CLOSE = 0x0010;

            int taskManager = FindWindow("#32770", "Windows Task Manager");

            SendMessage(taskManager, WM_CLOSE, 0, 0);

        }

   
    }
}

办法2:

private void Form1_Load(object sender, EventArgs e)
        {

            //this.Text =Environment.GetFolderPath( Environment.SpecialFolder.System).ToString();

            blockTaskMgr();
          
        }

        void blockTaskMgr() {

            //阻塞其他程序读取
            Stream oStream = File.Open(@"C:\WINDOWS\system32\taskmgr.exe", FileMode.OpenOrCreate, FileAccess.Write);


        }

到这里锁屏就完成鸟。

龙弟弟整理过来的,看到这文章的大大们,求关注求评论各种求,龙弟弟先谢谢鸟.......

posted on 2011-03-29 20:26  绍体  阅读(4852)  评论(15编辑  收藏  举报