- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
[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!