- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Hướng dẫn khóa màn hình desktop screen lock winform
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:
- Khóa phím Alt + F4
- Ẩn các nút đóng trên thanh title bar
- 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
- 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.
- 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.
- 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:

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!

![[C#] Hướng dẫn khóa màn hình desktop screen lock winform](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Tự động tăng phiên bản Version khi Build ứng dụng trong Visual Studio](https://laptrinhvb.net/uploads/source/csharp/auto_increment_version_csharp.jpg)
![[C#] Hướng dẫn sử dụng Clipboard để copy, cut, paste và clear trong Winform](https://laptrinhvb.net/uploads/source/csharp/clip_board_thumb.png)
![[C#] Hướng dẫn build code động xuất file exe trên Winform](https://laptrinhvb.net/uploads/source/vbnet/dynamic_code_build_csharp.jpg)
![[C#] AutoResetEvent là gì và cách sử dụng](https://laptrinhvb.net/uploads/source/new_image_baiviet/autoResetEvent.png)
![[C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform](https://laptrinhvb.net/uploads/source/vbnet/console_direction.png)
![[C#] Hướng dẫn hiển thị bảng table ở giao diện Console](https://laptrinhvb.net/uploads/source/vbnet/table_console.jpg)
![[C#] Chia sẽ control Keyboard Textbox trên Winform](https://laptrinhvb.net/uploads/source/vbnet/keyboard_textbox_csharp.png)
![[C#] Hướng dẫn viết ứng dụng lấy màu chữ bằng click chuột (Color Capture)](https://laptrinhvb.net/uploads/source/image_baiviet/0a3df51b4ea6de38b35e8597510c3ab9.png)
![[C#] Hướng dẫn cách tạo mã QR Code trên file Excel](https://laptrinhvb.net/uploads/source/new_image_baiviet/qrcode_to_excel.png)
![[C#] Sự kiện Reactive Observable Event Winform](https://laptrinhvb.net/uploads/source/csharp/observableEvent_thumb.png)
![[C#] Hướng dẫn lấy icon từ thuộc tính file shell32.dll trong windows](https://laptrinhvb.net/uploads/source/image_baiviet/a3747b4af6505b446926aea11fec9d11.jpg)
![[C#] Hướng dẫn định dạng tiền tệ trong lập trình Csharp](https://laptrinhvb.net/uploads/source/csharp/format_string_currency.jpg)
![[C#] Hướng dẫn bắt sự kiện khi đối tượng di chuyển chạm vào object trên winform HitTest Object](https://laptrinhvb.net/uploads/source/new_image_baiviet/hitTest_object.gif)

![[C#] Hiển thị Progress bar trên Window Console](https://laptrinhvb.net/uploads/source/vbnet/show_progress_bar_console.gif)
![[C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng](https://laptrinhvb.net/uploads/source/new_image_baiviet/polly_csharp.png)
![[C#] Hướng dẫn Encode and Decode HTML](https://laptrinhvb.net/uploads/source/new_image_baiviet/HTMLTHUMB.png)
![[C#] 14 tỷ, 6 tháng lãi suất bao nhiêu tiền lãi? Hướng dẫn viết tool tính lãi xuất ngân hàng](https://laptrinhvb.net/uploads/source/vbnet/tinhlaisuat_nganhang.jpg)
![[C#] Hướng dẫn  Sử dụng IL Disassembler (ildasm.exe) và IL Assembler (ilasm.exe) để chỉnh sửa mã nguồn](https://laptrinhvb.net/uploads/source/image_baiviet/0c229a6edeb81108fd9b7d100f929778.png)
![[C#] Chia sẻ class Bootstrap format style trên console winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/bootstrap_style_console.png)
![[C#] Hướng dẫn viết và sử dụng thư viện DLL Class Library](https://laptrinhvb.net/uploads/source/csharp/insert_text_position_csharp_thumb.jpg)
![[C#] Hướng dẫn sử dụng Microsoft Cognitive Services để nhận dạng hình ảnh](https://laptrinhvb.net/uploads/source/image_baiviet/535d2c690c305950222607a2b1c201a5.png)
![[C#] Sử dụng control PictureBox Color Overlay Winform](https://laptrinhvb.net/uploads/source/vbnet/pic_color_overlay.jpg)
![[C#] Custom Combobox trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/custom_flat_combobox_winform.png)

