[C#] Lắng nghe sự kiện System Event Windows trên winform
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.SystemEventsGiao 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);
}
}
}
Cảm ơn mọi người đã xem.
DOWNLOAD FULL SOURCE CODE
Theo LapTrinhVB
![[CSHARP] How to Capture System Events [CSHARP] How to Capture System Events](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3D0_I6zyIYyqzDqSs73kmQwy9k9WQjN0H8N6I7oz80eUMQwewrLuTZGVgr8I_ixbXI1e6afIMv3obyhMY0-MXId3inVaIQdHd-Nt9C6yULSXZ5p_pOhl46dxS3kcu86LAd0CTW-Sk_iBb667nGLp6Cs1DNrmrSrJNPM-qm7g2auafES2EcbRJ_EDmbfo/s16000/%5BCSHARP%5D%20How%20to%20Capture%20System%20Events.png)
