基于C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖
更新時(shí)間:2022年12月18日 08:41:18 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過(guò)程
效果
代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Point mouseOffset;//鼠標(biāo)位置 private bool isMouseDown = false;//表示鼠標(biāo)是否按下 private void pictureBox2_Click(object sender, EventArgs e) { Application.Exit();//窗體的關(guān)閉按鈕 } 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("請(qǐng)輸入密碼!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { if (txtPwd2.Text.Trim() == txtPwd.Text.Trim())//如果兩次密碼輸入一致 { Form2 frm2 = new Form2();//實(shí)例化Form2窗體 frm2.s = this.Size;//傳遞窗體大小 frm2.x = this.Location.X;//傳遞窗體的X坐標(biāo) frm2.y = this.Location.Y;//傳遞窗體的Y坐標(biāo) frm2.infos = txtInfo.Text.Trim();//傳遞掛機(jī)信息 frm2.pwd = txtPwd2.Text.Trim();//傳遞解鎖密碼 this.Hide();//隱藏當(dāng)前窗體 frm2.ShowDialog();//打開(kāi)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;//獲取鼠標(biāo)活動(dòng)的區(qū)域 public int x;//獲取鼠標(biāo)活動(dòng)區(qū)域的X坐標(biāo) public int y;//獲取鼠標(biāo)活動(dòng)區(qū)域的Y坐標(biāo) public string infos;//獲取掛機(jī)信息 public string pwd;//獲取解鎖密碼 myHook h = new myHook();//實(shí)例化公共類 private void Form2_Load(object sender, EventArgs e) { label1.Location = new Point(x,y-50);//設(shè)置顯示掛機(jī)信息的位置 label1.Text = infos;//顯示掛機(jī)信息 base.Opacity = 0.5;//設(shè)置掛機(jī)界面透明度 h.InsertHook();//安裝鉤子 } private void Form2_MouseMove(object sender, MouseEventArgs e) { Cursor.Clip = new Rectangle(x, y, s.Width, s.Height);//設(shè)置鼠標(biāo)活動(dòng)的區(qū)域 } private void Form2_MouseClick(object sender, MouseEventArgs e) { Form3 frm3 = new Form3();//實(shí)例化Form3窗體 frm3.x = x;//傳遞X坐標(biāo) frm3.y = y;//傳遞Y坐標(biāo) frm3.infos = infos;//傳遞掛機(jī)信息 frm3.pwd = pwd;//傳遞解鎖密碼 frm3.ShowDialog();//打開(kāi)解鎖界面 } private void timer1_Tick(object sender, EventArgs e) { Process[] p = Process.GetProcesses();//獲取所有系統(tǒng)運(yùn)行的進(jìn)程 foreach (Process p1 in p)//遍歷進(jìn)程 { try { //如果進(jìn)程中存在名為“taskmgr”,則說(shuō)明任務(wù)管理器已經(jīng)打開(kāi) if (p1.ProcessName.ToLower().Trim() == "taskmgr") { p1.Kill();//關(guān)掉任務(wù)管理器的進(jìn)程 Cursor.Clip = new Rectangle(x, y, s.Width, s.Height);//重新設(shè)置鼠標(biāo)活動(dòng)的區(qū)域 return; } } catch { return; } } } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { h.UnInsertHook();//卸載鉤子 timer1.Stop();//停止計(jì)時(shí)器 } }
public partial class Form3 : Form { public Form3() { InitializeComponent(); } public int x;//鼠標(biāo)活動(dòng)區(qū)域的X坐標(biāo) public int y;//鼠標(biāo)活動(dòng)區(qū)域的Y坐標(biāo) public string infos;//掛機(jī)界面顯示的信息 public string pwd;//解鎖密碼 private void Form3_Load(object sender, EventArgs e) { this.TopMost = true;//設(shè)置停靠在最前端 this.Location = new Point(x, y);//設(shè)置窗體位置 lblinfo.Text = infos;//顯示掛機(jī)信息 } private void button1_Click(object sender, EventArgs e) { if (txtPwd.Text.Trim() == pwd)//判斷輸入的密碼是否正確 { Application.Exit();//如果正確則退出掛機(jī)界面 } else//否則 { lblinfo.Text = "輸入解鎖密碼錯(cuò)誤,請(qǐng)重新輸入!";//提示錯(cuò)誤信息 } } private void button2_Click(object sender, EventArgs e) { this.TopMost = false;//取消??孔钋暗脑O(shè)置 this.Close();//關(guān)閉窗體 } }
public class myHook { private IntPtr pKeyboardHook = IntPtr.Zero;//鍵盤(pán)鉤子句柄 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);// 鉤子委托聲明 //鍵盤(pán)鉤子委托實(shí)例不能省略變量 private HookProc KeyboardHookProcedure; //底層鍵盤(pán)鉤子 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); //鍵盤(pán)鉤子處理函數(shù) 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; } }
到此這篇關(guān)于基于C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖的文章就介紹到這了,更多相關(guān)C#電腦系統(tǒng)掛機(jī)鎖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
C#實(shí)現(xiàn)設(shè)置或屏蔽熱鍵的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置或屏蔽熱鍵,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn)
這篇文章主要介紹了c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01C# 未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例
c#開(kāi)發(fā)過(guò)程中出現(xiàn)未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例,錯(cuò)誤一般是下面的原因,軟件中也是因?yàn)闆](méi)有獲取到數(shù)據(jù)導(dǎo)致,需要的朋友可以參考下2022-09-09