BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[CSHARP] How to Capture System Events

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.

[CSHARP] How to Capture System Events

[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.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);
        }
    }
}

Cảm ơn mọi người đã xem.

DOWNLOAD FULL SOURCE CODE


إرسال تعليق

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...