With WinForms it is easy:
but how to do it in WPF, what is the place for WndProc
C#
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);
}
}
}
Sam HobbsRank #107 · 20.5K reputationPosted Nov 5, 2023, 6:26 PM EST
Code Snippet
public partial class MainWindow : Window { private const int WM_CLOSE = 0x0010; public MainWindow() { InitializeComponent(); } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); WindowInteropHelper wih = new WindowInteropHelper(this); HwndSource source = HwndSource.FromHwnd(wih.Handle); source.AddHook(new HwndSourceHook(WndProc)); } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case WM_CLOSE: MessageBox.Show("Bye!"); break; default: break; } return IntPtr.Zero; } }Except you might need to do something different for the return value, it has been a few years since I did any of that. I leave that for you to find.