How to handle WndProc messages in WPF?

前端 未结 9 1005
不知归路
不知归路 2020-11-22 10:46

In Windows Forms, I\'d just override WndProc, and start handling messages as they came in.

Can someone show me an example of how to achieve the same thi

相关标签:
9条回答
  • 2020-11-22 11:15

    Actually, as far as I understand such a thing is indeed possible in WPF using HwndSource and HwndSourceHook. See this thread on MSDN as an example. (Relevant code included below)

    // 'this' is a Window
    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)
    {
        //  do stuff
    
        return IntPtr.Zero;
    }
    

    Now, I'm not quite sure why you'd want to handle Windows Messaging messages in a WPF application (unless it's the most obvious form of interop for working with another WinForms app). The design ideology and the nature of the API is very different in WPF from WinForms, so I would suggest you just familiarise yourself with WPF more to see exactly why there is no equivalent of WndProc.

    0 讨论(0)
  • 2020-11-22 11:15

    You can attach to the 'SystemEvents' class of the built-in Win32 class:

    using Microsoft.Win32;
    

    in a WPF window class:

    SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
    SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
    SystemEvents.SessionEnding += SystemEvents_SessionEnding;
    SystemEvents.SessionEnded += SystemEvents_SessionEnded;
    
    private async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        await vm.PowerModeChanged(e.Mode);
    }
    
    private async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        await vm.PowerModeChanged(e.Mode);
    }
    
    private async void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        await vm.SessionSwitch(e.Reason);
    }
    
    private async void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
    {
        if (e.Reason == SessionEndReasons.Logoff)
        {
            await vm.UserLogoff();
        }
    }
    
    private async void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e)
    {
        if (e.Reason == SessionEndReasons.Logoff)
        {
            await vm.UserLogoff();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 11:18

    If you don't mind referencing WinForms, you can use a more MVVM-oriented solution that doesn't couple service with the view. You need to create and initialize a System.Windows.Forms.NativeWindow which is a lightweight window that can receive messages.

    public abstract class WinApiServiceBase : IDisposable
    {
        /// <summary>
        /// Sponge window absorbs messages and lets other services use them
        /// </summary>
        private sealed class SpongeWindow : NativeWindow
        {
            public event EventHandler<Message> WndProced;
    
            public SpongeWindow()
            {
                CreateHandle(new CreateParams());
            }
    
            protected override void WndProc(ref Message m)
            {
                WndProced?.Invoke(this, m);
                base.WndProc(ref m);
            }
        }
    
        private static readonly SpongeWindow Sponge;
        protected static readonly IntPtr SpongeHandle;
    
        static WinApiServiceBase()
        {
            Sponge = new SpongeWindow();
            SpongeHandle = Sponge.Handle;
        }
    
        protected WinApiServiceBase()
        {
            Sponge.WndProced += LocalWndProced;
        }
    
        private void LocalWndProced(object sender, Message message)
        {
            WndProc(message);
        }
    
        /// <summary>
        /// Override to process windows messages
        /// </summary>
        protected virtual void WndProc(Message message)
        { }
    
        public virtual void Dispose()
        {
            Sponge.WndProced -= LocalWndProced;
        }
    }
    

    Use SpongeHandle to register for messages you're interested in and then override WndProc to process them:

    public class WindowsMessageListenerService : WinApiServiceBase
    {
        protected override void WndProc(Message message)
        {
            Debug.WriteLine(message.msg);
        }
    }
    

    The only downside is that you have to include System.Windows.Forms reference, but otherwise this is a very encapsulated solution.

    More on this can be read here

    0 讨论(0)
提交回复
热议问题