views:

21

answers:

1

Windows Forms have the IMessageFilter interface to capture messages. How is this done in WPF? Specifically, I want to create a clipboard format listener.

+1  A: 

In your Window derived class:

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        ((HwndSource)PresentationSource.FromVisual(this)).AddHook(myHook)
    }

    private IntPtr myHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            // process messages here
            default:
                return IntPtr.Zero;
        }
    }
Nir