C#实现Window系统桌面锁定效果
				
									
					
					
						|  | 
							admin 2025年5月10日 18:33
								本文热度 2094 | 
					
				 
				在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);        }    }                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("密码错误");        }        return;    }
    [DllImport("user32.dll")]    private static extern bool SetForegroundWindow(IntPtr hWnd);    [DllImport("user32.dll")]    private static extern IntPtr GetForegroundWindow();}
    该案例通过强制某个窗口显示在最前面(即便是其他程序窗口在运行)。通过定时器定时检测当前活动窗口,判断用户是否试图切换到其他窗口,如果是则强制切回锁定窗口,达到锁定屏幕的效果。
该文章在 2025/5/10 18:33:20 编辑过