Sign in to follow this  
rich135

SimConnect from Windows Presentation Foundation (WPF)

Recommended Posts

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

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

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;}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this