C# 实现锁屏

首先,将窗体的FormBorderStyle设置为none,WindowState设为Maximized 让窗体占据整个页面。

form窗体代码:
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();
        }


    }
}
新建一个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键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。 
        HookProc KeyBoardHookProcedure;
        //键盘Hook结构函数 
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        #region DllImport
        //设置钩子 
        [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)]
        //抽掉钩子 
        public static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll")]
        //调用下一个钩子 
        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 自定义事件
        public void Hook_Start()
        {
            // 安装键盘钩子 
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);


                hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
                           KeyBoardHookProcedure,
                          GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                //如果设置钩子失败. 
                if (hHook == 0)
                {
                    Hook_Clear();
                    throw new Exception();
                }
                else
                {

                }
            }
        }

        //取消钩子事件 
        public void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            //如果去掉钩子失败. 
            if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
        }

        //这里可以添加自己想要的信息处理 
        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键
                    case 91://左徽标键
                    case 92://右徽标键
                    case 93://鼠标右键快捷键
                    case 164://
                    case 9://TAB键
                    case 10://Shift键
                    case 17://Ctrl键
                    case 18://Alt键
                    case 162://
                    case 110://.键
                    case 46://Delete键
                    case 115://F4键
                    case 241://F1键
                        return 1;
                }
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        #endregion

    }
}

为了美观,窗体用到了一些控件,需要加一些背景图片放在Resoures文件夹中,当点击左下角的Win图片时会显示“关机”“关于”否则是处于隐藏状态。





猜你喜欢

转载自blog.csdn.net/duyusean/article/details/79059715
今日推荐