基于C#實現(xiàn)電腦系統(tǒng)掛機鎖
更新時間:2022年12月18日 08:41:18 作者:芝麻粒兒
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)電腦系統(tǒng)掛機鎖,文中的示例代碼講解詳細,對我們學(xué)習(xí)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();//窗體的關(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("請輸入密碼!", "提示", 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();//隱藏當(dāng)前窗體
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;//獲取鼠標活動的區(qū)域
public int x;//獲取鼠標活動區(qū)域的X坐標
public int y;//獲取鼠標活動區(qū)域的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);//設(shè)置顯示掛機信息的位置
label1.Text = infos;//顯示掛機信息
base.Opacity = 0.5;//設(shè)置掛機界面透明度
h.InsertHook();//安裝鉤子
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Clip = new Rectangle(x, y, s.Width, s.Height);//設(shè)置鼠標活動的區(qū)域
}
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();//獲取所有系統(tǒng)運行的進程
foreach (Process p1 in p)//遍歷進程
{
try
{
//如果進程中存在名為“taskmgr”,則說明任務(wù)管理器已經(jīng)打開
if (p1.ProcessName.ToLower().Trim() == "taskmgr")
{
p1.Kill();//關(guān)掉任務(wù)管理器的進程
Cursor.Clip = new Rectangle(x, y, s.Width, s.Height);//重新設(shè)置鼠標活動的區(qū)域
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;//鼠標活動區(qū)域的X坐標
public int y;//鼠標活動區(qū)域的Y坐標
public string infos;//掛機界面顯示的信息
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;//顯示掛機信息
}
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;//取消??孔钋暗脑O(shè)置
this.Close();//關(guān)閉窗體
}
}
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);
//鍵盤鉤子處理函數(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#實現(xiàn)電腦系統(tǒng)掛機鎖的文章就介紹到這了,更多相關(guān)C#電腦系統(tǒng)掛機鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實現(xiàn)
這篇文章主要介紹了c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

