C# 實現鎖屏
阿新 • • 發佈:2019-01-08
新建一個HOOK的類,設定鉤子using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Net; using System.IO; using System.Security.Cryptography; using System.Drawing.Drawing2D; using System.Management; using System.Management.Instrumentation; namespace UI { public partial class Form1 : Form { Hook h = new Hook(); public Form1() { InitializeComponent(); } string password; private void Form1_Load(object sender, EventArgs e) { FullScreen(); Locations(); txt_pwd.Focus(); lb_name.Text = this.HostName; showhead(); h.Hook_Start(); tm_kill.Start(); WritePwd(); } public void showhead()//頭像顯示 { if (File.Exists(@"C:\Windows\System32\Face.bmp")) { pic_head.ImageLocation = @"C:\Windows\System32\Face.bmp"; } else { pic_head.Image = UI.Properties.Resources.WIN8Head; } } string pwd2 = null; protected override bool ProcessKeyEventArgs(ref Message m)//禁用工作管理員 { KillTaskmgr(); return base.ProcessKeyEventArgs(ref m); } protected override void WndProc(ref Message m)//禁用滑鼠右鍵 { if (m.Msg == 0x205) { } base.WndProc(ref m); } public void WritePwd()//寫配置檔案 { if (File.Exists(@"C:\Windows\System32\lock.pwd")) { password = File.ReadAllText(@"C:\Windows\System32\lock.pwd", Encoding.Default); if (File.Exists(@"C:\Windows\System32\unlock.ID")) { pwd2 = File.ReadAllText(@"C:\Windows\System32\unlock.ID", Encoding.Default); } else pwd2 = null; } else { password = MD5(UI.frmLogin.PWD); } } public string HostName//本機名 { get { // string hostname = Dns.GetHostName(); string hostname = Convert.ToString( UI.frmLogin.CardID); return hostname; } } public void Locations()//控制元件相對於螢幕位置 { Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this); int width = ScreenArea.Width; //螢幕寬度 int height = ScreenArea.Height; pl_login.Location = new Point((width - 480) / 2, (height - 200) / 2); bt_about1.Location = new Point(bt_about1.Location.X, height - 50); pl_about.Location = new Point(pl_about.Location.X, height - 140); } private void KillTaskmgr()//禁用工作管理員 { Process[] sum = Process.GetProcesses(); foreach (Process p in sum) { if (p.ProcessName == "taskmgr" || p.ProcessName == "cmd") try { p.Kill(); } catch { ; } } } private void FullScreen()//全屏 { this.SetVisibleCore(true); } //根據自己的需要新增需要實現的功能 private void bt_login_Click(object sender, EventArgs e)//登入解鎖 { if (pwd2 != null) { if (txt_pwd.Text.Trim() == Convert.ToString(UnlockID(), 16).ToUpper()) { pwd2 = null; MessageBox.Show("PIN碼解鎖成功!密碼重置為123", "提示"); return; } } if (txt_pwd.Text != "") { if (MD5(txt_pwd.Text) == password) { pwd2 = null; h.Hook_Clear(); // Application.Exit(); //關閉程式 bool status = false; bool flag = new Facade.ConsumerFacade().ModifyLineLog(UI.frmLogin.CardID, status);//設定掛機狀態 this.Hide(); } else { pl_info.Visible = true; tm_info.Start(); txt_pwd.Text = ""; } } } private void timer1_Tick(object sender, EventArgs e)//開始置頂 { KillTaskmgr(); } public static string MD5(string word)//MD5加密 { MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(word)); return System.Text.Encoding.Default.GetString(result); } #region//滑鼠點選空白部分設定消失 private void Form1_Click(object sender, EventArgs e) { this.AcceptButton = bt_login; } #endregion private void tm_info_Tick(object sender, EventArgs e)//提示資訊消失 { pl_info.Visible = false; tm_info.Stop(); } private void bt_about1_Click(object sender, EventArgs e) { if (pl_about.Visible == false) pl_about.Visible = true; else pl_about.Visible = false; } public string GetMoAddress()// 獲取網絡卡硬體地址 { return null; } private void bt_about_Click(object sender, EventArgs e)//關於 { MessageBox.Show("軟體名稱:天天見網咖收費系統\n\n作者:杜小魚\n\n版本:1.0\n\n鎖屏密碼與登入密碼一致\n\n有BUG請發BUG出現的詳細情況至\n郵箱
[email protected]以便改進", "關於", MessageBoxButtons.OK); } public long UnlockID()//返回PIN碼 { long a = 123; return a; } private void bt_shutdown_Click(object sender, EventArgs e)//關機 { DialogResult dr = MessageBox.Show("確定要關機?", "提示", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK) ApiCalls.ShutDown(); } } }