- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[C#] Lắng nghe sự kiện System Event Windows trên winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách bắt sự kiện Windows System trên lập trình C#, Winform.
[C#] How to Capture System Events
Microsoft cung cấp cho chúng ta một class để chúng ta sử dụng để bắt sự kiện trên Windows: Microsoft.Win32.SystemEvents
Giao diện demo ứng dụng C#:
Ở hình trên, các bạn thấy khi mình thay đổi: Độ phân giải màn hình, ngày giờ hệ thống... thì nó bắt sự kiện và show ra cho mình biết.
Dưới đây, là các sự kiện của lớp SystemEvents:
Name | Description |
DisplaySettingsChanged |
Occurs when the user changes the display settings. |
DisplaySettingsChanging |
Occurs when the display settings are changing. |
EventsThreadShutdown |
Occurs before the thread that listens for system events is terminated. |
InstalledFontsChanged |
Occurs when the user adds fonts to or removes fonts from the system. |
LowMemory |
Obsolete. Occurs when the system is running out of available RAM. |
PaletteChanged |
Occurs when the user switches to an application that uses a different palette. |
PowerModeChanged |
Occurs when the user suspends or resumes the system. |
SessionEnded |
Occurs when the user is logging off or shutting down the system. |
SessionEnding |
Occurs when the user is trying to log off or shut down the system. |
SessionSwitch |
Occurs when the currently logged-in user has changed. |
TimeChanged |
Occurs when the user changes the time on the system clock. |
TimerElapsed |
Occurs when a windows timer interval has expired. |
UserPreferenceChanged |
Occurs when a user preference has changed. |
UserPreferenceChanging |
Occurs when a user preference is changing. |
Full source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SystemEventListener
{
public partial class Form1 : Form
{
private bool eventHandlersCreated;
public Form1()
{
InitializeComponent();
}
private void btnListen_Click(object sender, EventArgs e)
{
this.StartListening();
}
private void btnStop_Click(object sender, EventArgs e)
{
this.StopListening();
}
private void StartListening()
{
Microsoft.Win32.SystemEvents.InstalledFontsChanged += new EventHandler(FontHandler);
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(ScreenHandler);
Microsoft.Win32.SystemEvents.TimeChanged += new EventHandler(TimeHandler);
Microsoft.Win32.SystemEvents.UserPreferenceChanged += new Microsoft.Win32.UserPreferenceChangedEventHandler(PreferenceChangedHandler);
this.eventHandlersCreated = true;
}
private void StopListening()
{
Microsoft.Win32.SystemEvents.InstalledFontsChanged -= new EventHandler(FontHandler);
Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= new EventHandler(ScreenHandler);
Microsoft.Win32.SystemEvents.TimeChanged -= new EventHandler(TimeHandler);
Microsoft.Win32.SystemEvents.UserPreferenceChanged -= new Microsoft.Win32.UserPreferenceChangedEventHandler(PreferenceChangedHandler);
this.eventHandlersCreated = false;
}
private void FontHandler(object sender, EventArgs e)
{
txtLog.Text += string.Format("Installed fonts changed. {0}", Environment.NewLine);
}
private void PreferenceChangedHandler(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
{
txtLog.Text += string.Format("You changed a setting: {0} {1}", e.Category.ToString(), Environment.NewLine);
}
private void ScreenHandler(object sender, EventArgs e)
{
txtLog.Text += string.Format("Screen resolution changed. {0}", Environment.NewLine);
}
private void TimeHandler(object sender, EventArgs e)
{
txtLog.Text += string.Format("System time changed. {0}", Environment.NewLine);
}
}
}
Thanks for watching!