October 23, 200619 yr The SimConnect SDK examples for .NET are written for Windows Form Applications. The secret sauce for processing events is: protected override void DefWndProc(ref Message m) { if (m.Msg == WM_USER_SIMCONNECT) { if (simconnect != null) { simconnect.ReceiveMessage(); } } else { base.DefWndProc(ref m); } }This code overrides the default windows procedure to intercept WM_USER_SIMCONNECT messages.Does anyone know how to do this in WPF (aka Avalon) applications? There is no DefWndProc type override in a WPF window. After hours of research, I've come up empty.ThanksRich Lucas
October 30, 200619 yr Author After a few days of experimenting, I am finally SimConnected from a WPF application. The secret sauce is:1. Use WindowInteropHelper class to get a handle to the app's main window. WindowInteropHelper wih = new WindowInteropHelper(this.MainWindow);2. Obtain the window handle (hwnd) from WindowInteropHelper. hwnd = wih.Handle;3. Create a HwndSource class from the handle. HwndSource hs = HwndSource.FromHwnd(hwnd);4. Use HwndSource.AddHook() to intercept Win32 messages. hs.AddHook(new HwndSourceHook(ProcessSimConnectWin32Events))5. Create a message handler.private IntPtr ProcessSimConnectWin32Events( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){ handled = false; if (msg == WM_SIMCONNECT_EVENT) { if (connected && simconnect != null) { simconnect.ReceiveMessage(); handled = true; } } return (IntPtr)0;}
Create an account or sign in to comment