Rob*_*ean 131
您可以通过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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
完全取自优秀博客文章:在Steve Rands的WPF应用程序中使用自定义WndProc
Nol*_*rin 58
实际上,据我所知,这样的事情确实可以在WPF中使用HwndSource和HwndSourceHook.请参阅MSDN上的这个线程作为一个例子.(下面包括相关代码)
// 'this' is a Window
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// do stuff
return IntPtr.Zero;
}
Run Code Online (Sandbox Code Playgroud)
现在,我不太清楚你为什么要在WPF应用程序中处理Windows Messaging消息(除非它是使用另一个WinForms应用程序的最明显的互操作形式).在WPF和WinForms中,API的设计思想和性质是非常不同的,所以我建议你更熟悉WPF,以确切了解为什么没有WndProc的等价物.
sof*_*erx 15
HwndSource src = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
src.AddHook(new HwndSourceHook(WndProc));
.......
public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if(msg == THEMESSAGEIMLOOKINGFOR)
{
//Do something here
}
return IntPtr.Zero;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89137 次 |
| 最近记录: |