C# 实现锁屏

article/2025/7/17 7:41:48
首先,将窗体的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);}elsepwd2 = 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;}#endregionprivate 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;elsepl_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邮箱18731680163@163.com以便改进", "关于", 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图片时会显示“关机”“关于”否则是处于隐藏状态。






http://chatgpt.dhexx.cn/article/2Du6pC3W.shtml

相关文章

win11中睡眠唤醒后如何禁止锁屏界面及登录界面

第一步禁止锁屏界面 gpedit.msc 不显示锁屏-》已启用 第二步:不显示登陆框 netplwiz 经过以上设置以后,打开笔记本就可以直接显示桌面了。

办公计算机锁屏方法,电脑快速锁屏,办公室神技能,再也不怕别人看我的电脑了!...

原标题:电脑快速锁屏,办公室神技能,再也不怕别人看我的电脑了! 现在快节奏的社会,电脑的更方面性能与功能是非常之大,我们对电脑的使用频率也越来越高,可见它对我们生活越来越重要。在我们平时使…

Android锁屏的解锁(九个点),使用画的方式,大致的步骤

这篇博客是全部代码(没有解释,纯代码): https://blog.csdn.net/weixin_44614751/article/details/103101104 效果: 第一步:添加背景 第二步:创建九个点 先读取每一个点的图片: creatDot 创建九个点: initNin

ubuntu 18.04取消自动锁屏功能

有时候几分钟不用Ubuntu,系统就自动锁屏了,这是一种安全措施,防止别人趁你不在时使用你的系统。但对于大部分人而言,这是没有必要的,尤其是Ubuntu虚拟机,里面没啥重要的东西,每次锁屏后需要重新…

Android 通知栏,锁屏播放音乐,类似音乐播放器

项目中需要用到播放音频的功能,想做一个类似酷狗、酷我这样的音频播放功能,在通知栏和锁屏时都可以操控音乐,开发中发现oppo reno手机在锁屏时不显示通知栏,研究了整整一天终于解决,特作记录,给遇到同样问题…

Android自定义锁屏实现----仿正点闹钟滑屏解锁

本文原创,转载请注明出处:http://blog.csdn.net/qinjuning 前几周看了下解锁的框架,基本上算是弄了个脸熟。看着别人花哨的解锁界面,心里也很痒痒的。于是,画了一天时间, 捣鼓出了这个成果----仿正点闹钟解…

易安卓打开Android系统中的解锁方式选择页面(锁屏方式选择)

感谢名单 感谢fylfyl2写的https://blog.csdn.net/fyilun/article/details/21257595 E4A打开锁屏方式页面 Intent intent new Intent(); ComponentName cm new ComponentName("com.android.settings","com.android.settings.ChooseLockGeneric"); inte…

Android锁屏的解锁(九个点),使用画的方式

一、效果展示: 这篇博客有解释大概的步骤: https://blog.csdn.net/weixin_44614751/article/details/103101199 二、代码部分: MainActivity.java中的代码: package com.example.drawunlock1;import androidx.appcompat.app.AppCompatActivity;import android.content.re…

让电脑不被锁屏的方法,亲测有效

通过JS来控制键盘,定时按下SCROLLLOCK键,达到电脑不会被锁屏的效果。 通常公司电脑都会自动锁屏,只是时间有长短。有时候闲着了不用电脑,但是却不想让电脑锁屏。那么可以用js代码来控制键盘的按键循环按下实现不锁屏的效果&#x…

android 强制锁屏app,自制力app强制锁屏

自制力app强制锁屏非常适合在学习工作中没有自律性的用户们,当打开app后开启锁屏状态,手机就打不开了,重启也不可能解除锁屏;在此期间,就可以免于手机的打扰,专注学习;感兴趣的小伙伴们快来下载…

android系统密码设置功能,手机锁屏密码怎么设置 三种安卓手机锁屏方式推荐

手机中有很多应用都是与金钱挂钩,特别是微信与支付宝等等既涉及到隐私又与财产关联,这是后手机的安全就尤为重要的,而手机的锁屏密码就是一道最基本的防护措施,那么手机锁屏密码怎么设置?来看看小编推荐的三种安卓手机锁屏方式吧…

Mac锁屏的几种方式

刚换了工作,公司里给配了MacBook,第一次使用,很多常见操作都不知道快捷键,今天来记录下锁屏的几种方式:电脑为MacBook Pro,OS为MacOS Sierra 10.12.3 1. ctrl shift 右上角开关机键 2. option comm…

C语言for循环结构经典练习

文章目录 一、for循环基本知识二、经典例题及解析1.水仙花数2.求规定范围内的完数3.求规定范围内质数4.计算阶乘之和5.计算55555555555555(类型)6.计算112123123412345(类型)7.判断用户输入正整数的位数8.判断某正整数是否为回文数9.九九乘法表10.统计用户输入的字符中&#xf…

Java基础语法——循环结构

每日正能量 趁你现在还有时间,尽你自己最大的努力,努力做成你最想做的那件事,成为你最想成为的那种人,过着你最想过的那种生活。这个世界永远比你想的要更精彩,不要败给生活。 循环结构 【本章内容】1. while循环 2. d…

Python - 循环结构

循环结构 🐍While循环🐍While…else…循环🐍for循环🐍for…else…循环🐍循环体结束语句🐍嵌套循环 本次主要介绍的是程序的循环结构逻辑。 循环就是按照一定的条件重复的去做一件事情,当条件不成…

While循环结构

1.while循环语句 1.1循环概念 循环是程序设计语言中反复执行某些代码的一种计算机处理过程 1.2 while循环的作用 重复执行某些代码 1.3 while循环的基本语法 while 条件: ____反复执行的代码 …… while及while中的代码块就是while循环代码块。看以下示例&#…

三种循环结构

循环结构:循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。它由循环体中的条件,判断继续执行某个功能还是退出循环。根据判断条件,循环结构又可细分为以下两种形式:先判断后执行的循环结构和先执行后判断的循环结构。下面将对各个循环结…

c语言中循环结构有什么作用,浅谈C语言中循环结构程序设计

高茂婵 吕雪 彭星星 孙新杰 摘要:现在人们对计算机中的算法的要求越来越高,顺序结构已经满足不了人们的需求,我们需要探索更高层次的操作算法。在程序设计中,我们操作的时候经常遇到需要重复执行的情况,而循环结构就恰恰满足了这个要求。在程序设计中,循环结构是算法中必…

Python循环结构

今天我们讲一下python的循环结构,习题比较多,大家多联系,有问题可以给我留言。 目录 一、Python中循环的介绍1、什么是循环?2、循环的作用3、循环的种类 二、while循环基本语法及其应用1、while循环的基本语法2、while循环的执行…

LabVIEW循环结构

LabVIEW可提供For循环和While循环两种循环结构。For循环必须指定循环总次数,达到指定循环次数后程序会自动退出循环;而While循环则不用指定循环次数,只需要指定循环退出条件,如果循环退出条件成立,则退出循环。所以知道…