狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#實(shí)現(xiàn)Window系統(tǒng)桌面鎖定效果

admin
2025年5月10日 18:33 本文熱度 72
前言

在C# 中如何實(shí)現(xiàn)Windows 系統(tǒng)鎖定屏幕的效果,本文通過使用Windows API 中的設(shè)置前臺窗口方法SetForegroundWindow和獲取前臺窗口方法GetForegroundWindow方法實(shí)現(xiàn)。

SetForegroundWindow:是將指定的窗口(通過其句柄 hWnd)設(shè)置為前臺窗口,并激活它(即獲得焦點(diǎn))。

方法簽名:

private static extern bool SetForegroundWindow(IntPtr hWnd);

IntPtr:hWnd 目標(biāo)窗口的句柄。

return:true   成功設(shè)置窗口為前臺窗口。

return:false  失敗(可能由于權(quán)限不足或窗口不可見)。

GetForegroundWindow:的作用是獲取當(dāng)前處于前臺活動狀態(tài)的窗口的句柄。

方法簽名:

private static extern IntPtr GetForegroundWindow();

IntPtr:hWnd 當(dāng)前處于前臺活動狀態(tài)的窗口。

通過上面的方法僅是設(shè)置活動窗口,鎖定桌面。那么如何解除鎖定呢?最簡單的方式當(dāng)然是通過按鈕關(guān)閉,但是又不能如此簡單,那就是加密碼判斷,所以需要添加按鈕和文本框,輸入正確的密碼解除鎖定。

 為了更炫酷一點(diǎn)的,程序中還添加一個密碼面板區(qū)域?qū)崟r移動顯示效果。


功能

1、程序運(yùn)行鎖定屏幕。

2、輸入密碼解鎖屏幕。

3、初始密碼123456。


功能預(yù)覽
由于運(yùn)行程序后無法截圖,所以不展示運(yùn)行效果。



代碼

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;            // 應(yīng)用新位置            panel.Location = new Point(newX, newY);        }));    }    /// <summary>    /// 強(qiáng)制切回當(dāng)前窗口    /// </summary>    private void Timer_Tick(object sender, EventArgs e)    {        this.Invoke(new Action(() =>        {            //獲獲取當(dāng)前處于前臺(活動狀態(tài))的窗口            //如果當(dāng)前程序不是前臺窗口,設(shè)置當(dāng)前程序窗口為前臺窗口。            if (GetForegroundWindow() != this.Handle)            {                SetForegroundWindow(this.Handle);            }        }));    }    /// <summary>    ///  強(qiáng)制當(dāng)前窗口到前臺    /// </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();}

總結(jié)

    該案例通過強(qiáng)制某個窗口顯示在最前面(即便是其他程序窗口在運(yùn)行)。通過定時器定時檢測當(dāng)前活動窗口,判斷用戶是否試圖切換到其他窗口,如果是則強(qiáng)制切回鎖定窗口,達(dá)到鎖定屏幕的效果。


該文章在 2025/5/10 18:33:20 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊、財務(wù)費(fèi)用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved