首頁 > 軟體

基於C#實現電腦系統掛機鎖

2022-12-19 14:01:23

實踐過程

效果

程式碼

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#電腦系統掛機鎖內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com