c# - 如何在 WPF 中处理 WndProc 消息?

标签 c# wpf wndproc

在 Windows 窗体中,我只是覆盖 WndProc,并在消息传入时开始处理它们。

谁能给我一个例子,说明如何在 WPF 中实现同样的事情?

最佳答案

您可以通过 System.Windows.Interop 命名空间执行此操作,该命名空间包含名为 HwndSource 的类。

使用这个的例子

using System;
using System.Windows;
using System.Windows.Interop;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            // Handle messages...

            return IntPtr.Zero;
        }
    }
}

完全取自优秀博文:Using a custom WndProc in WPF apps by Steve Rands

关于c# - 如何在 WPF 中处理 WndProc 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/624367/

相关文章:

c# - 关于 ViewModel 和 View 交互

c# - xaml 中带有 TreeViewItems 的 ListView

C# 绘图 : What is the best way to draw a polygon with a hole in the middle

c# - WPF如何获得最大化/最小化按钮图标 - 自定义标题栏

C# 判断表单是否最大化

c++ - 为对话框创建窗口类

c# - 在NAudio中设置播放开始/结束点

c# - Linq连接两个相同类型的对象

c# - 找不到 IronPython sys._getframe

vb.net - WndProc WM_NCMOUSEUP 不火