none
WPF and WndProc question.

    Question

  • I want my WPF based app to listen to events from the OS (i.e. 0x0002 for window destroy, etc).

    In Windows Forms programming - I simply override WndProc.

    In WPF - I've implemented the following


    private void loadedForm(object sender, RoutedEventArgs e)


    HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
    source.AddHook(new HwndSourceHook(WndProc));
    }



    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {

    if (msg == WM_DESTROY)
              {
                    MessageBox.Show("I saw a WM_DESTROY!");
                    handled = true;
              } 

    return IntPtr.Zero;
    }

    For obious reasons - I only get the message box when I close MY application. How would I implement an application (WPF based) to listent to system events (i.e. detect closing of windows for ALL windows and not just my app). I also want to detect other messages so utilizing WndProc is pretty much my only option. Help greatly appreciated.
    Monday, August 06, 2007 11:59 PM

Answers

All replies

  •  

    At present, I known maybe you can use win32 hook like this:

    support.microsoft.com/kb/Q318804

     

    But maybe there is more better mthod thait I don't known.

    Tuesday, August 07, 2007 1:21 AM
  • I have tried using your code.
    only that I used WM_QUERYENDSESION.
    I was trying to imitate the behavior where user shutsdown/restart/logoff,
    my application propts for "Do you want to save changes?" then close the file i'm using.
    However nothing happens when I click yes/no, my application does not close. and if I try to close it again using the close button, I receive an error? why is this so? is it because of the Hook??

    Friday, July 17, 2009 1:21 AM
  • About WM_QUERYENDSESION, I want to cancel the closing of Windows
    Can u help me?
    Friday, November 06, 2009 11:30 AM