本文告诉大家如何添加窗口消息钩子
窗口消息钩子需要先拿到窗口指针然后需要在窗口初始化完成之后才可以做到,推荐的做法请看代码
代码语言:javascript复制 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;
}