LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#实现Window系统桌面锁定效果

admin
2025年5月10日 18:33 本文热度 107
前言

在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();}

总结

    该案例通过强制某个窗口显示在最前面(即便是其他程序窗口在运行)。通过定时器定时检测当前活动窗口,判断用户是否试图切换到其他窗口,如果是则强制切回锁定窗口,达到锁定屏幕的效果。


该文章在 2025/5/10 18:33:20 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved