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