With WinForms it is easy:
but how to do it in WPF, what is the place for WndProc
namespace WinFormsApp1 { public partial class Form1 : Form { private static int WM_QUERYENDSESSION = 0x11; public Form2() { InitializeComponent(); } protected override void WndProc(ref Message m) { if (m.Msg == WM_QUERYENDSESSION) { MessageBox.Show("logoff, shutdown, or reboot"); } base.WndProc(ref m); } } }