A Coder
A Coder

Reputation: 3046

WndProc in RadWindow WPF

I'm trying to detect the Windows LogOff or Shutdown in my WPF application. Can someone help?

xaml.cs

private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
public static event Microsoft.Win32.SessionEndingEventHandler SessionEnding;

protected virtual void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == WM_QUERYENDSESSION)
    {
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be  
    // raised in the base WndProc.  
    base.WndProc(ref m);  //Error

} //WndProc

Error: RadWindow does not contain a definition for 'WndProc'

Upvotes: 0

Views: 336

Answers (1)

mm8
mm8

Reputation: 169160

You should be able to get a reference to the parent WPF window and create a Win32 hook once your RadWindow has been loaded:

public class MyRadWindow : RadWindow
{
    public MyRadWindow()
    {
        Loaded += MyRadWindow_Loaded;
    }

    private void MyRadWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        System.Windows.Window wpfWindow = this.ParentOfType<System.Windows.Window>();
        HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(wpfWindow).Handle);
        source.AddHook(new HwndSourceHook(WndProc));
    }

    private const int WM_QUERYENDSESSION = 0x11;
    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == WM_QUERYENDSESSION)
        {
            //...
        }

        return IntPtr.Zero;
    }

    ...
}

Upvotes: 1

Related Questions