基于C#实现电脑系统挂机锁
作者:芝麻粒儿
这篇文章主要为大家详细介绍了如何利用C#实现电脑系统挂机锁,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
实践过程
效果
代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Point mouseOffset;//鼠标位置 private bool isMouseDown = false;//表示鼠标是否按下 private void pictureBox2_Click(object sender, EventArgs e) { Application.Exit();//窗体的关闭按钮 } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X; yOffset = -e.Y; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = false; } } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } private void button1_Click(object sender, EventArgs e) { if (txtPwd.Text.Trim() == "" || txtPwd2.Text.Trim() == "")//判断是否输入密码 { MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { if (txtPwd2.Text.Trim() == txtPwd.Text.Trim())//如果两次密码输入一致 { Form2 frm2 = new Form2();//实例化Form2窗体 frm2.s = this.Size;//传递窗体大小 frm2.x = this.Location.X;//传递窗体的X坐标 frm2.y = this.Location.Y;//传递窗体的Y坐标 frm2.infos = txtInfo.Text.Trim();//传递挂机信息 frm2.pwd = txtPwd2.Text.Trim();//传递解锁密码 this.Hide();//隐藏当前窗体 frm2.ShowDialog();//打开Form2窗体 } else { MessageBox.Show("两次密码不一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } } private void Form1_Load(object sender, EventArgs e) { } }
public partial class Form2 : Form { public Form2() { InitializeComponent(); } public Size s;//获取鼠标活动的区域 public int x;//获取鼠标活动区域的X坐标 public int y;//获取鼠标活动区域的Y坐标 public string infos;//获取挂机信息 public string pwd;//获取解锁密码 myHook h = new myHook();//实例化公共类 private void Form2_Load(object sender, EventArgs e) { label1.Location = new Point(x,y-50);//设置显示挂机信息的位置 label1.Text = infos;//显示挂机信息 base.Opacity = 0.5;//设置挂机界面透明度 h.InsertHook();//安装钩子 } private void Form2_MouseMove(object sender, MouseEventArgs e) { Cursor.Clip = new Rectangle(x, y, s.Width, s.Height);//设置鼠标活动的区域 } private void Form2_MouseClick(object sender, MouseEventArgs e) { Form3 frm3 = new Form3();//实例化Form3窗体 frm3.x = x;//传递X坐标 frm3.y = y;//传递Y坐标 frm3.infos = infos;//传递挂机信息 frm3.pwd = pwd;//传递解锁密码 frm3.ShowDialog();//打开解锁界面 } private void timer1_Tick(object sender, EventArgs e) { Process[] p = Process.GetProcesses();//获取所有系统运行的进程 foreach (Process p1 in p)//遍历进程 { try { //如果进程中存在名为“taskmgr”,则说明任务管理器已经打开 if (p1.ProcessName.ToLower().Trim() == "taskmgr") { p1.Kill();//关掉任务管理器的进程 Cursor.Clip = new Rectangle(x, y, s.Width, s.Height);//重新设置鼠标活动的区域 return; } } catch { return; } } } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { h.UnInsertHook();//卸载钩子 timer1.Stop();//停止计时器 } }
public partial class Form3 : Form { public Form3() { InitializeComponent(); } public int x;//鼠标活动区域的X坐标 public int y;//鼠标活动区域的Y坐标 public string infos;//挂机界面显示的信息 public string pwd;//解锁密码 private void Form3_Load(object sender, EventArgs e) { this.TopMost = true;//设置停靠在最前端 this.Location = new Point(x, y);//设置窗体位置 lblinfo.Text = infos;//显示挂机信息 } private void button1_Click(object sender, EventArgs e) { if (txtPwd.Text.Trim() == pwd)//判断输入的密码是否正确 { Application.Exit();//如果正确则退出挂机界面 } else//否则 { lblinfo.Text = "输入解锁密码错误,请重新输入!";//提示错误信息 } } private void button2_Click(object sender, EventArgs e) { this.TopMost = false;//取消停靠最前的设置 this.Close();//关闭窗体 } }
public class myHook { private IntPtr pKeyboardHook = IntPtr.Zero;//键盘钩子句柄 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);// 钩子委托声明 //键盘钩子委托实例不能省略变量 private HookProc KeyboardHookProcedure; //底层键盘钩子 public const int idHook = 13; //安装钩子 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)] public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr pInstance, int threadId); //卸载钩子 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle); //键盘钩子处理函数 private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG)); if (pKeyboardHook != IntPtr.Zero) { switch (((Keys)m.vkCode)) { case Keys.LWin: case Keys.RWin: case Keys.Delete: case Keys.Alt: case Keys.Escape: case Keys.F4: case Keys.Control: case Keys.Tab: return 1; } } return 0; } //安装钩子 public bool InsertHook() { IntPtr pIn = (IntPtr)4194304; if (this.pKeyboardHook == IntPtr.Zero) { this.KeyboardHookProcedure = new HookProc(KeyboardHookProc); this.pKeyboardHook = SetWindowsHookEx(idHook, KeyboardHookProcedure, pIn, 0); if (this.pKeyboardHook == IntPtr.Zero) { this.UnInsertHook(); return false; } } return true; } //卸载钩子 public bool UnInsertHook() { bool result = true; if (this.pKeyboardHook != IntPtr.Zero) { result = (UnhookWindowsHookEx(this.pKeyboardHook) && result); this.pKeyboardHook = IntPtr.Zero; } return result; } [StructLayout(LayoutKind.Sequential)] public struct KeyMSG { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } }
到此这篇关于基于C#实现电脑系统挂机锁的文章就介绍到这了,更多相关C#电脑系统挂机锁内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!