WPF通过钩子监听当前进程windows消息

using System.Windows.Interop;

this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized);

 

protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource != null)
                hwndSource.AddHook(new HwndSourceHook(this.WndProc));
        }

 

        void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            IntPtr hwnd = new WindowInteropHelper(this).Handle;
            HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc1));
        }

        private IntPtr WndProc1(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //实现了禁止双击标题栏的消息    
            if (msg == 0x00A3)
            {
                handled = true;
                wParam = IntPtr.Zero;
            }
            else if (msg == 74)
            {
                this.Result.Text += "\r\n74";
            }

            return IntPtr.Zero;
        }

posted @ 2020-11-28 14:26  棂信  阅读(524)  评论(0编辑  收藏  举报