第一步:将窗体的FormBorderStyle设置为none,WindowState设为Maximized
占据整个屏幕。
第二步:使用钩子监控全局键盘事件。即屏蔽掉大部分系统热键。但是屏蔽ctrl+alt+del 任务管理器则较复杂,这个特例后面讨论。
使用全局钩子应该注意的地方:将代码放到一个独立的类库里面(只有dll才能被注射到其他进程中)。
using System.Collections.Generic; |
using System.Runtime.InteropServices; |
namespace KeyboardHookDLL |
public class KeyboardHook |
public delegate int KeyboardProc( int nCode, IntPtr wParam, IntPtr lParam); |
static int hKeyboardHook = 0; |
KeyboardProc KeyboardHookProcedure; |
/// 钩子函数,需要引用空间(using System.Reflection;) |
/// 线程钩子监听键盘消息设为2,全局钩子监听键盘消息设为13 |
/// 线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14 |
public const int WH_KEYBOARD = 13; |
public const int WH_MOUSE_LL = 14; |
public struct KeyboardMSG |
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; |
private const int WH_KEYBOARD_LL = 13; |
private const int WH_MOUSE = 7; |
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; |
/// vs2008中的声明方法,在vs2010中略有不同 |
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)); |
(( 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 CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); |
public void KeyMaskStart() |
KeyboardHookProcedure = new KeyboardProc(KeyboardHookProc); |
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProcedure, |
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); |
throw new Exception( "SetWindowsHookEx failed." ); |
////用二进制流的方法打开任务管理器。而且不关闭流.这样任务管理器就打开不了 |
public void KeyMaskStop() |
retKeyboard = UnhookWindowsHookEx(hKeyboardHook); |
throw new Exception( "UnhookWindowsHookEx failed." ); |
第三步:这个时候你会发现,程序能屏蔽大部分的热键了,唯独ctrl+alt+del,你无论怎么去拦截,只要你按下这三个键,任务管理器活灵活现的出来了。
原因:这个组合键是系统级别滴(唯一的,ctrl+shift+esc不是)。普通的软件拦截是木有用的。得驱动级啥的才行吧。
既然如此,就只能转换思路了。既然不能治本,治标也行,只要能达到目的。
思路有两个:1是让任务管理器一旦运行就被结束,让用户不能操作。2是让任务管理器以另外的方式先运行并隐藏起来,用户就不能运行了。
思路1的代码:使用timer控件
private void timer1_Tick( object sender, EventArgs e) |
Process[] p = Process.GetProcesses(); |
foreach (Process p1 in p) |
if (p1.ProcessName.ToLower().Trim() == "taskmgr" ) |
思路2有两个比较好的办法
还有个不咋滴的办法,就是以文件流的方式打开taskmgr.exe(代码在上面的KeyboardHookDLL中能找到),在xp下还行,到win7下面就会有提示了。
办法1:以隐藏的方式运行。
using System.Collections.Generic; |
using System.ComponentModel; |
using System.Windows.Forms; |
using System.Diagnostics; |
using System.Runtime.InteropServices; |
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( |
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; |
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) |
Stream oStream = File.Open( @"C:\WINDOWS\system32\taskmgr.exe" , FileMode.OpenOrCreate, FileAccess.Write); |