基於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#電腦系統掛機鎖內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!