利用C#實(shí)現(xiàn)Window系統(tǒng)桌面鎖定效果
前言
在實(shí)際開發(fā)中,我們有時(shí)需要實(shí)現(xiàn)類似"屏幕鎖定"的效果,比如用于演示程序、臨時(shí)權(quán)限控制、或者個(gè)人興趣項(xiàng)目。
C# 作為一門強(qiáng)大的桌面應(yīng)用開發(fā)語言,結(jié)合 Windows API 可以輕松實(shí)現(xiàn)這一功能。
本文將通過調(diào)用 SetForegroundWindow 和 GetForegroundWindow 兩個(gè)核心方法,實(shí)現(xiàn)一個(gè)簡易但實(shí)用的屏幕鎖定程序,并支持輸入正確密碼解鎖。此外,為了提升交互體驗(yàn),還為密碼面板增加了實(shí)時(shí)移動(dòng)動(dòng)畫效果。
功能概述
該程序具備以下核心功能:
1、啟動(dòng)后立即鎖定整個(gè)屏幕,阻止用戶切換到其他應(yīng)用程序;
2、輸入正確密碼(默認(rèn)為 123456)可解除鎖定;
3、密碼輸入框區(qū)域具有動(dòng)態(tài)移動(dòng)效果,增加趣味性和視覺吸引力;
4、界面無邊框、半透明設(shè)計(jì),模擬真實(shí)鎖屏風(fēng)格。
*注意:由于運(yùn)行后無法截圖,因此文中不附運(yùn)行效果圖,建議讀者親自運(yùn)行代碼查看效果。
技術(shù)點(diǎn)
1、獲取與設(shè)置前臺(tái)窗口
我們使用了兩個(gè)重要的 Windows API 方法來實(shí)現(xiàn)窗口強(qiáng)制聚焦功能:
GetForegroundWindow()
作用:獲取當(dāng)前處于前臺(tái)活動(dòng)狀態(tài)的窗口句柄。
返回值類型:IntPtr
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
SetForegroundWindow(IntPtr hWnd)
作用:將指定窗口設(shè)為前臺(tái)窗口,并獲得焦點(diǎn)。
參數(shù)說明:hWnd 是目標(biāo)窗口的句柄。
返回值:true 成功;false 失?。ㄈ绱翱诓豢梢娀驒?quán)限不足)
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
這兩個(gè)函數(shù)結(jié)合定時(shí)器使用,可以持續(xù)檢測并強(qiáng)制用戶停留在我們的鎖定窗口上。
2、程序初始化設(shè)置
在窗體加載事件中完成以下配置:
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 = FormBorderStyle.None; // 無邊框
this.Location = new Point((Screen.PrimaryScreen.Bounds.Width - 400) / 2,
(Screen.PrimaryScreen.Bounds.Height - 300) / 2); // 居中顯示
}
設(shè)置窗體為最大全屏、無邊框、半透明,模擬鎖屏背景;
密碼輸入框居中顯示;
添加回車鍵監(jiān)聽,方便快速解鎖。
3、定時(shí)檢測并強(qiáng)制切回鎖定窗口
我們使用 System.Timers.Timer 每隔 100ms 檢測一次當(dāng)前前臺(tái)窗口是否是我們自己的程序,如果不是,則強(qiáng)制切回來:
private void Timer_Tick(object sender, EventArgs e)
{
this.Invoke(new Action(() =>
{
if (GetForegroundWindow() != this.Handle)
{
SetForegroundWindow(this.Handle);
}
}));
}
這樣可以有效防止用戶通過 Alt+Tab 或點(diǎn)擊任務(wù)欄切換窗口。
4、動(dòng)態(tài)移動(dòng)密碼面板
為了讓界面更有趣味性,我們給密碼面板添加了一個(gè)簡單的運(yùn)動(dòng)動(dòng)畫:
private void TimerMove_Elapsed(object sender, 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);
}));
}
使用定時(shí)器每 30ms 更新一次位置;
遇到邊界自動(dòng)反彈,形成類似"彈球"效果;
增強(qiáng)視覺吸引力,避免界面過于單調(diào)。
完整代碼示例
以下是完整的窗體類代碼:
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 = FormBorderStyle.None;
this.Location = new 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);
}
}
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);
}));
}
private void Timer_Tick(object sender, EventArgs e)
{
this.Invoke(new Action(() =>
{
if (GetForegroundWindow() != this.Handle)
{
SetForegroundWindow(this.Handle);
}
}));
}
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("密碼錯(cuò)誤");
}
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
}
總結(jié)
通過本文的學(xué)習(xí),我們掌握了如何利用 C# 結(jié)合 Windows API 來實(shí)現(xiàn)一個(gè)簡單的屏幕鎖定程序,并通過定時(shí)器機(jī)制保持鎖定狀態(tài),防止用戶切換窗口。
主要知識(shí)點(diǎn)包括:
使用
SetForegroundWindow和GetForegroundWindow控制窗口焦點(diǎn);利用定時(shí)器實(shí)現(xiàn)動(dòng)態(tài)行為和窗口監(jiān)控;
使用 WinForm 的窗體屬性打造仿系統(tǒng)鎖屏效果;
提供密碼驗(yàn)證機(jī)制增強(qiáng)安全性。
該項(xiàng)目雖然小巧,但涵蓋了多個(gè)實(shí)用技巧,適合作為 C# 初學(xué)者的練手項(xiàng)目,也適合進(jìn)階開發(fā)拓展更多功能,例如:
加入圖形驗(yàn)證碼;
支持多用戶登錄;
更加復(fù)雜的 UI 動(dòng)畫;
日志記錄與錯(cuò)誤提示等。
最后
到此這篇關(guān)于利用C#實(shí)現(xiàn)Window系統(tǒng)桌面鎖定效果的文章就介紹到這了,更多相關(guān)C# Window桌面鎖定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過C#解析HTML進(jìn)行文本提取和結(jié)構(gòu)化數(shù)據(jù)獲取的操作指南
在 .NET 項(xiàng)目開發(fā)中,HTML 內(nèi)容解析與數(shù)據(jù)提取是極為常見的開發(fā)需求,Free Spire.Doc for .NET 作為一款免費(fèi)的 .NET 文檔處理組件,提供了便捷的 HTML 解析接口,本文將結(jié)合實(shí)際代碼,詳細(xì)講解該組件在 C# 中讀取 HTML 的具體實(shí)現(xiàn),需要的朋友可以參考下2026-01-01
詳解WPF如何顯示具有層級(jí)關(guān)系的數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了在WPF中如何顯示具有層級(jí)關(guān)系的數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
C#控制臺(tái)實(shí)現(xiàn)簡單飛行棋游戲
這篇文章主要為大家詳細(xì)介紹了C#控制臺(tái)實(shí)現(xiàn)簡單飛行棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C#實(shí)現(xiàn)高性能寫入txt大量數(shù)據(jù)
在 C# 中高性能寫入大量數(shù)據(jù)到文本文件時(shí),需結(jié)合 ?流式處理、內(nèi)存優(yōu)化和系統(tǒng)級(jí)技巧?,本文為大家介紹了針對(duì)超大規(guī)模數(shù)據(jù)的深度優(yōu)化方案,需要的可以參考一下2025-05-05

