C# implements lock screen

First, set the FormBorderStyle of the form to none and the WindowState to Maximized so that the form occupies the entire page.

form form code:
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()// Avatar display
        {
            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)//Disable task manager
        {
            KillTaskmgr ();
            return base.ProcessKeyEventArgs(ref m);
        }

        protected override void WndProc(ref Message m)//Disable the right mouse button
        {
            if (m.Msg == 0x205)
            {

            }
            base.WndProc(ref m);
        }

        public void WritePwd()//Write configuration file
        {
            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//This machine name
        {
            get
            {
               // string hostname = Dns.GetHostName();
                string hostname = Convert.ToString (UI.frmLogin.CardID);
                return hostname;

            }
        }

        public void Locations()//The position of the control relative to the screen
        {
            Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
            int width = ScreenArea.Width; //Screen 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()//Disable Task Manager
        {
            Process[] sum = Process.GetProcesses();
            foreach (Process p in sum)
            {
                if (p.ProcessName == "taskmgr" || p.ProcessName == "cmd")
                    try
                    {
                        p.Kill();
                    }
                    catch
                    {
                        ;
                    }
            }
        }

        private void FullScreen()//Full screen
        {
            this.SetVisibleCore(true);
        }
       //Add the functions that need to be implemented according to your own needs
        private void bt_login_Click(object sender, EventArgs e)//Login unlock
        {
            if (pwd2 != null)
            {
                if (txt_pwd.Text.Trim() == Convert.ToString(UnlockID(), 16).ToUpper())
                {
                    pwd2 = null;
                    MessageBox.Show("PIN code unlocked successfully! Password reset to 123", "Prompt");
                    return;
                }
            }
            if (txt_pwd.Text != "")
            {
                if (MD5(txt_pwd.Text) == password)
                {
                    pwd2 = null;
               
                    h.Hook_Clear();
                  // Application.Exit(); //Close the program
                    bool status = false;
                    bool flag = new Facade.ConsumerFacade().ModifyLineLog(UI.frmLogin.CardID, status);//Set on-hook status
                    this.Hide();
                }
                else
                {
                    pl_info.Visible = true;
                    tm_info.Start();
                    txt_pwd.Text = "";
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)//Start to top
        {
            KillTaskmgr ();
        }

        public static string MD5(string word)//MD5 encryption
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(word));
            return System.Text.Encoding.Default.GetString(result);
        }


        #region//The setting of the blank part of the mouse click disappears
        private void Form1_Click(object sender, EventArgs e)
        {
            this.AcceptButton = bt_login;
        }
        #endregion

        private void tm_info_Tick(object sender, EventArgs e)//The prompt information disappears
        {
            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()// Get the hardware address of the network card   
        {
            return null;
        }

        private void bt_about_Click(object sender, EventArgs e)//关于
        {
            MessageBox.Show("Software Name: Everyday See Internet Cafe Charging System\n\nAuthor: Du Xiaoyu\n\nVersion: 1.0\n\nThe lock screen password is the same as the login password\n\nIf there is a bug, please send a bug The details appear to\nmail [email protected] for improvement", "About", MessageBoxButtons.OK);
        }

        public long UnlockID()//Return PIN code
        {

            long a = 123;
            return a;
        }

        private void bt_shutdown_Click(object sender, EventArgs e)//关机
        {
            DialogResult dr = MessageBox.Show("Are you sure you want to shut down?", "Prompt", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
                ApiCalls.ShutDown();
        }


    }
}
Create a new HOOK class and set the 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.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;

namespace UI
{
    public class Hook
    {
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;
        //LowLevel keyboard interception, if it is WH_KEYBOARD=2, the system keyboard cannot be intercepted, Acrobat Reader will obtain the keyboard before you intercept it.
        HookProc KeyBoardHookProcedure;
        //Keyboard Hook structure function
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        #region DllImport
        //set the hook
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        // remove the hook
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll")]
        // call the next hook
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        #endregion

        #region Custom Events
        public void Hook_Start()
        {
            // install keyboard hook
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);


                hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
                           KeyBoardHookProcedure,
                          GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                //If setting the hook fails.
                if (hHook == 0)
                {
                    Hook_Clear();
                    throw new Exception();
                }
                else
                {

                }
            }
        }

        // cancel the hook event
        public void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            //If removing the hook fails.
            if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
        }

        //Here you can add the information processing you want
        public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {

            if (nCode >= 0)
            {
                KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));

                int v = kbh.vkCode;
                switch (v)
                {
                    case 27://ESC key
                    case 91://left logo key
                    case 92://right logo key
                    case 93://right mouse button shortcut
                    case 164://
                    case 9://TAB键
                    case 10://Shift键
                    case 17://Ctrl key
                    case 18://Alt key
                    case 162://
                    case 110://.key
                    case 46: // Delete Key
                    case 115://F4 key
                    case 241://F1 key
                        return 1;
                }
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        #endregion

    }
}

In order to be beautiful, the form uses some controls, you need to add some background pictures and put them in the Resoures folder. When you click the Win picture in the lower left corner, it will display "shutdown" and "about", otherwise it will be hidden.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472641&siteId=291194637