导入win32api程序
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace ScreenLock
8 {
9 class Win32Api
10 {
11
12
13 //设置钩子
14 [DllImport("user32.dll")]
15 public static extern int SetWindowsHookEx(int idHook,HookProc lpfn,IntPtr hInstance,int threadID);
16 //卸载钩子
17 [DllImport("user32.dll,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall")]
18 public static extern bool UnHookWindowsHookEx(int idHook);
19 //调用下一个钩子
20 [DllImport("user32.dll")]
21 public static extern int CallNextHookEx(int idHook, int nCode,int wParam,IntPtr lParam);
22 //返回当前线程ID值
23 [DllImport("kernel32.dll")]
24 public static extern int GetCurrentThreadId();
25
26 //返回指定模块的句柄
27 [DllImport("kernel32.dll")]
28 public static extern IntPtr GetModuleHandle(string name);
29 }
30 }
程序锁定模块
View Code
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ScreenLock
{
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);//定义钩子过程委托(钩子函数原型)
public partial class LockForm : Form
{
public LockForm()
{
InitializeComponent();
HookStart();//启动Hook,截获键盘事件
}
private void button1_Click(object sender, EventArgs e)
{
//密码正确,将将窗体以淡出形式退出
ControlOpacity.Enabled = true;
}
//下面定义键盘钩子的相关内容,用于截获键盘消息
#region
HookProc KeyBoardProcedure;
static int hHook = 0;//钩子函数的句柄
public const int WH_KEYBOARD = 13;
//钩子结构函数
public struct KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
//安装键盘钩子
public void HookStart()
{
if (hHook == 0)
{
//实例化一个HookProc对象
KeyBoardProcedure = new HookProc(LockForm.KeyBoardHookProc);
//创建线程钩子
hHook = Win32Api.SetWindowsHookEx(WH_KEYBOARD, KeyBoardProcedure, Win32Api.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
//如果设置线程钩子失败
if (hHook == 0)
{
HookClear();
}
}
}
//取消钩子
public void HookClear()
{
bool rsetKeyboard = true;
if (hHook != 0)
{
rsetKeyboard = Win32Api.UnHookWindowsHookEx(hHook);
hHook = 0;
}
if (!rsetKeyboard)
{
throw new Exception("取消钩子失败!");
}
}
//对截获的键盘操作的处理
public 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.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;
}
//......可以在这里做更多处理
}
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
#endregion
//控制窗体透明度
private void ControlOpacity_Tick_1(object sender, EventArgs e)
{
if (this.Opacity <= 0.1)
{
this.Close();
}
else
{
this.Opacity = this.Opacity - 0.1;
}
}
private void KillTaskmgr_Tick(object sender, EventArgs e)
{
Process[] remoteAll = Process.GetProcessesByName("taskmgr");
foreach (Process s in remoteAll )
{
s.Kill();
}
}
}
}