NEWS

[C#] Hướng dẫn khóa màn hình desktop screen lock winform

[C#] Hướng dẫn khóa màn hình desktop screen lock winform
Đăng bởi: Thảo Meo - Lượt xem: 5501 14:01:43, 08/12/2020C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách viết ứng dụng khóa màn hình Screen lock desktop trong lập trình c# winform.

[C#] Lock Screen Desktop Winform

Để lock được screen desktop của của người dùng, các bạn cần thực hiện các bước sau:

  1. Khóa phím Alt + F4
  2. Ẩn các nút đóng trên thanh title bar
  3. Dùng 1 timer để khi có ứng dụng nào xuất hiện lên trước, mình sẽ dùng thuộc tính Bringtofront để mang ứng dụng lock mình lên trên cùng
  4. Gởi phím sendkey ESC, chặn người dùng mở start menu trên Windows (vì trên windows start, người dùng có thể sử dụng lệnh Taskkill trên MS-DoS để đóng ứng dụng.
  5. Nếu các bạn mình khóa cẩn thận (tích hợp chức năng khởi động ứng dụng cùng windows) thì người dùng có tắt máy mở lại ứng dụng vẫn tiếp tục chạy và khóa màn hình lại.
  6. Khi nhập mật khẩu sai quá 3 lần, thì chúng ta sẽ ẩn luôn TextBox nhập mật khẩu để mở khóa

Dưới đây, là hình ảnh demo ứng dụng Screen Lock C#, Winform:

screen_lock_desktop

Full source code lock sreen c#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ScreenLock
{
    public partial class SimpleLock : Form
    {
        public SimpleLock()
        {
            InitializeComponent();
            this.BackColor = Color.Black;
            this.CausesValidation = false;
            this.StartPosition = FormStartPosition.Manual;
            this.WindowState = FormWindowState.Maximized;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.BackgroundImage = Image.FromFile("bg.jpg");          
            txtPassword.TextAlign = HorizontalAlignment.Center;
            txtPassword.BackColor = Color.DarkGray;
            txtPassword.ForeColor = Color.Yellow;
            txtPassword.BorderStyle = BorderStyle.None;
            layerTimer.Interval = 1;
            layerTimer.Enabled = true;
          

        }
        protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
        {
            switch ((keyData))
            {

                case Keys.Control:
                    {
                        return true;
                    }

                case Keys.Alt | Keys.F4:
                    {
                        return true;
                    }

                case Keys.Alt | Keys.Control | Keys.Delete:
                    {
                        return true;
                    }

                case Keys.Control | Keys.Q:
                    {
                        return true;
                    }
            }
            return base.ProcessDialogKey(keyData);
        }

        int tries;
        bool approveClose;

        private void Form1_Load(object sender, EventArgs e)
        {

            txtPassword.Left = (int)(this.Width / 2) - (int)(txtPassword.Width / 2);
            txtPassword.Top = (int)(this.Height / 2) - (int)(txtPassword.Height / 2);
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                if (txtPassword.Text == "laptrinhvb")
                {
                    approveClose = true;
                    this.Close();
                }
                else
                {
                    tries++;
                    txtPassword.Text = string.Empty;

                    if (tries >= 3)
                    {
                        txtPassword.Enabled = false;
                        txtPassword.Visible = false;
                    }
                }
            }
        }

        private void SimpleLock_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F7 && tries >= 3)
            {

                tries = 0;
                txtPassword.Enabled = true;
                txtPassword.Visible = true;
                txtPassword.Focus();
            }
        }

        private void layerTimer_Tick(object sender, EventArgs e)
        {
            this.BringToFront();
         SendKeys.Send("{ESC}");
            if (!txtPassword.Focused) txtPassword.Focus();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
    }
}

Mật khẩu đóng ứng dụng demo app trên là: laptrinhvb 

p/s: nhớ đừng nhập mật khẩu sai quá 3 lần nhé các bạn.

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn khóa màn hình desktop screen lock winform
Đăng bởi: Thảo Meo - Lượt xem: 5501 14:01:43, 08/12/2020C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.