Wpf和WinForm在窗体中处理用户自定义消息的不同方式
2013-01-08 15:19 panmin 阅读(1344) 评论(0) 收藏 举报WPF:
在Visual的句柄创建后(如OnLoad、OnSourceInitialized代码里),使用下面方法:
方法一:
HwndSource source =PresentationSource.FromVisual(this) as HwndSource; //添加命名空间System.Windows.Interop
if(source != null) source.AddHook(WndProc);
方法二:
HwndSource source =HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if(source != null) source.AddHook(WndProc);
注:两种方法中的this可换成Visual对象。
然后就可以HwndSourceHook委托的WndProc方法了:
private IntPtr WndProc(IntPtr hwnd,int msg,IntPtr wParam,IntPtr lParam,ref bool handled)
{
// Handle messages... return IntPtr.Zero;
}
WinForm:
protected override void DefWndProc(ref Message m)
{
base.DefWndProc(ref m);
}
浙公网安备 33010602011771号