-
前言
-
在C# 中如何实现Windows 系统锁定屏幕的效果。
-
本文通过使用Windows API 中的设置前台窗口方法SetForegroundWindow和获取前台窗口方法GetForegroundWindow方法实现。
-
SetForegroundWindow: 是将指定的窗口(通过其句柄 hWnd)设置为前台窗口,并激活它(即获得焦点)。
-
方法签名:
private static extern bool SetForegroundWindow(IntPtr hWnd);- IntPtr: hWnd 目标窗口的句柄。
- return: true 成功设置窗口为前台窗口。
- return: false 失败(可能由于权限不足或窗口不可见)。
-
GetForegroundWindow: 的作用是获取当前处于前台活动状态的窗口的句柄。
-
方法签名:
private static extern IntPtr GetForegroundWindow(); -
IntPtr: hWnd 当前处于前台活动状态的窗口。
-
通过上面的方法仅是设置活动窗口,锁定桌面。那么如何解除锁定呢?最简单的方式当然是通过按钮关闭,但是又不能如此简单,那就是加密码判断,所以需要添加按钮和文本框,输入正确的密码解除锁定。
-
为了更炫酷一点的,程序中还添加一个密码面板区域实时移动显示效果。
-
-
功能
- 1、程序运行锁定屏幕。
- 2、输入密码解锁屏幕。
- 3、初始密码123456。
-
功能预览
- 由于运行程序后无法截图,所以不展示运行效果。
-
代码
public partial class FrmLockScreen : Form { private System.Timers.Timer timer; private System.Timers.Timer timerMove; private int speedX = 2; private int speedY = 1; public FrmLockScreen() { InitializeComponent(); } private void FrmLockScreen_Load(object sender, EventArgs e) { this.Activated += FrmLockScreen_Activated; this.WindowState = FormWindowState.Maximized; this.Opacity = 0.5D; this.TopMost = true; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Location = new System.Drawing.Point( (Screen.PrimaryScreen.Bounds.Width - 400) / 2, (Screen.PrimaryScreen.Bounds.Height - 300) / 2); this.panel.BackColor = SystemColors.Window; this.tbx_Password.KeyDown += FrmLockScreen_KeyDown; timer = new System.Timers.Timer(); timer.Interval = 100; timer.Elapsed += Timer_Tick; timer.Start(); timerMove = new System.Timers.Timer(); timerMove.Interval = 30; timerMove.Elapsed += TimerMove_Elapsed; timerMove.Start(); } private void FrmLockScreen_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { UnlockButton_Click(this,null); } } /// <summary> /// 定时更新密码框面板位置 /// </summary> private void TimerMove_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { panel.Invoke(new Action(() => { // 更新位置 int newX = panel.Location.X + speedX; int newY = panel.Location.Y + speedY; // 边界检测 if (newX <= 0 || newX + panel.Width >= this.ClientSize.Width) speedX = -speedX; if (newY <= 0 || newY + panel.Height >= this.ClientSize.Height) speedY = -speedY; // 应用新位置 panel.Location = new Point(newX, newY); })); } /// <summary> /// 强制切回当前窗口 /// </summary> private void Timer_Tick(object sender, EventArgs e) { this.Invoke(new Action(() => { //获获取当前处于前台(活动状态)的窗口 //如果当前程序不是前台窗口,设置当前程序窗口为前台窗口。 if (GetForegroundWindow() != this.Handle) { SetForegroundWindow(this.Handle); } })); } /// <summary> /// 强制当前窗口到前台 /// </summary> private void FrmLockScreen_Activated(object sender, EventArgs e) { SetForegroundWindow(this.Handle); } private void UnlockButton_Click(object sender, EventArgs e) { if (tbx_Password.Text == "123456") { timer.Stop(); timerMove.Stop(); this.Close(); } else { MessageBox.Show("密码错误"); } return; } [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); }
-
总结
- 该案例通过强制某个窗口显示在最前面(即便是其他程序窗口在运行)。通过定时器定时检测当前活动窗口,判断用户是否试图切换到其他窗口,如果是则强制切回锁定窗口,达到锁定屏幕的效果。
-
最后
- 如果你觉得这篇文章对你有帮助,不妨点个赞再走呗!
- 如有其他疑问,欢迎评论区留言讨论!
- 也可以加入微信公众号 [编程笔记in] ,一起交流学习!
❖ 感 谢 您 的 关 注 ❖