本帖最后由 wtujoxk 于 2014-8-18 16:21 编辑
可屏蔽键盘上的任意按键及组合按键(包括ALT+F4,任务管理器)
可在键盘鼠标不进行任何动作是锁屏
提示:解锁密码为8888
[C#] 纯文本查看 复制代码 using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace LockScreen
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
InitButtonTextbox();
this.TopMost = true;
}
int texPwdMax = 0; //判断密码输到第几位个数
const int PwdFigure = 4; //密码位数
const int ButtonFigure = 10; //按钮
const int PasswordFigure = 5; //文本框,文本框命名是从1开始
public System.Windows.Forms.Button[] bnt = new System.Windows.Forms.Button[ButtonFigure];
public System.Windows.Forms.TextBox[] txtPwd = new System.Windows.Forms.TextBox[PasswordFigure];
string Password = "8888"; //密码
int NumPwd; //判断按钮按下的数值
frmLock frmlock = new frmLock();//锁屏窗口
MyHook myhook = new MyHook();//屏蔽键盘
private void MainForm_Load(object sender, EventArgs e)
{
frmlock.Show();
ComputerDormancy ComputerDormancyConfig = new ComputerDormancy(this); //计算要休眠时间
//InitMainform initMainformConfig = new InitMainform(this);//实现具有Aero效果的窗体
myhook.InsertHook();
}
//按钮文本框
private void InitButtonTextbox()
{
for (int i = 0; i < ButtonFigure; i++)
{
Control[] c = this.Controls.Find("btn" + i.ToString(), true);
if (0 < c.Length)
{
bnt[i] = (System.Windows.Forms.Button)c[0];
}
}
for (int j = 1; j < PasswordFigure; j++)
{
Control[] c1 = this.Controls.Find("texPwd" + j.ToString(), true);
if (0 < c1.Length)
{
txtPwd[j] = (System.Windows.Forms.TextBox)c1[0];
}
}
}
//文本框输入的数值
private void texPwdAcceptNum()
{
texPwdMax++;
if (PwdFigure <= texPwdMax)
texPwdMax = PwdFigure;
for (int i = 1; i <= PwdFigure; i++)
{
if (i == texPwdMax)
{
if (string.IsNullOrEmpty(txtPwd[i].Text))
{
txtPwd[i].Text = NumPwd >= 0 && NumPwd <= 9 ? NumPwd.ToString() : string.Empty;
txtPwd[i].BackColor = System.Drawing.Color.Red;
}
}
}
MatchingPassWord();
}
//得到密码并比较
private void MatchingPassWord()
{
string Pwd = texPwd1.Text + texPwd2.Text + texPwd3.Text + texPwd4.Text;
if (PwdFigure == texPwdMax)
{
if (Pwd == Password)
{
this.Hide();
frmlock.Hide();
//this.WindowState = FormWindowState.Minimized;
//frmlock.WindowState = FormWindowState.Minimized;
LockTimer.Enabled = true;
texPwdMax = 0;
for (int i = 1; i <= PwdFigure; i++)
{
txtPwd[i].Text = string.Empty;
txtPwd[i].BackColor = System.Drawing.Color.White;
}
//myhook.UnHook();
}
else
{
MessageBox.Show("您输入的密码错误,请重新输入");
texPwdMax = 0;
for (int i = 1; i <= PwdFigure; i++)
{
txtPwd[i].Text = string.Empty;
txtPwd[i].BackColor = System.Drawing.Color.White;
}
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
myhook.UnHook();
Application.Exit();
}
//按钮事件
private void btnText_Click(object sender, EventArgs e)
{
Button btn = (sender as Button);
NumPwd = int.Parse(btn.Text);
texPwdAcceptNum();
}
//重置
private void btnAsterisk_Click(object sender, EventArgs e)
{
texPwdMax = 0;
for (int i = 1; i <= PwdFigure; i++)
{
txtPwd[i].Text = string.Empty;
txtPwd[i].BackColor = System.Drawing.Color.White;
}
}
//取消
private void btnCancel_Click(object sender, EventArgs e)
{
texPwdMax--;
if (0 > texPwdMax)
texPwdMax = 0;
for (int i = 0; i <= PwdFigure; i++)
{
if (i == texPwdMax)
if (!string.IsNullOrEmpty(txtPwd[i + 1].Text))
{
txtPwd[i + 1].Text = string.Empty;
txtPwd[i + 1].BackColor = System.Drawing.Color.White;
}
}
}
//阻止进程运行
private void TaskmgrTimer_Tick(object sender, EventArgs e)
{
//RunContentJudge();
Process[] p = Process.GetProcesses();//获取所有系统运行的进程
foreach (Process p1 in p)//遍历进程
{
try
{
if (p1.ProcessName.ToLower().Trim() == "taskmgr")//如果进程中存在名为“taskmgr”,则说明任务管理器已经打开
{
p1.Kill();//关掉任务管理器的进程
return;
}
}
catch
{ }
}
}
//如果鼠标键盘都没有动作的时候,运行锁屏
private void RunContentJudge()
{
if (ComputerDormancy.GetLastInputTime() >= LockTimer.Interval)
{
this.Show();
frmlock.Show();
}
else
LockTimer.Enabled = false;
}
//设定的时间内锁屏
private void LockTimer_Tick(object sender, EventArgs e)
{
myhook.InsertHook();
this.Show();
frmlock.Show();
LockTimer.Enabled = false;
}
}
}
|