2019-11-12-WPF-添加窗口消息钩子方法

title author date CreateTime categories
WPF 添加窗口消息钩子方法
lindexi
2019-11-12 18:46:53 +0800
2019-06-05 17:10:48 +0800
WPF

本文告诉大家如何添加窗口消息钩子

窗口消息钩子需要先拿到窗口指针然后需要在窗口初始化完成之后才可以做到,推荐的做法请看代码

        public MainWindow()
        {
            InitializeComponent();

            SourceInitialized += OnSourceInitialized;
        }

        private void OnSourceInitialized(object sender, EventArgs e)
        {
            var windowInteropHelper = new WindowInteropHelper(this);
            var hwnd = windowInteropHelper.Handle;

            HwndSource source = HwndSource.FromHwnd(hwnd);
            source.AddHook(Hook);
        }

        private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
        {
            return IntPtr.Zero;
        }
posted @ 2019-11-21 16:04  lindexi  阅读(49)  评论(0编辑  收藏  举报