P3D v4 [C#] WPF, DefWndProc in a WPF application

#1
Hi, while trying to add a new feature to my KsimSaver, i am struggling to find a way to use DefWndProc in WPF.
this code is from the SDK sample, as that is Windows Forms, this code is not usable for WPF, atleast the Protected override void DefWndProc(ref Message m) {} part. the rest of the simconnect code should work perfectly fine.

C#:
protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_USER_SIMCONNECT)
            {
                if (simconnect != null)
                {
                    simconnect.ReceiveMessage();
                }
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }
If anyone has any tips or ideas on how to implement this in WPF please let me know.
 
#2
To fix this issue i did the following:
setup a correct handle and add a hook when connecting to SimConnect :

C#:
private HwndSource gHs;
C#:
 WindowInteropHelper lWih = new WindowInteropHelper(Application.Current.MainWindow);
                    IntPtr lHwnd = lWih.Handle;

                    gHs = HwndSource.FromHwnd(lHwnd);

                    gHs.AddHook(new HwndSourceHook(WndProc));

                    // the constructor is similar to SimConnect_Open in the native API
                    simconnect = new SimConnect("KsimSaver", lHwnd, WM_USER_SIMCONNECT, null, 0);
then did the following in WndProc:

C#:
 private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
         
            handled = false;

            // if message is coming from simconnect and the connection is not null;
            // continue and receive message
            if (msg == WM_USER_SIMCONNECT && simconnect != null)
            {
                simconnect.ReceiveMessage();

               
                handled = true;
            }
           
            return (IntPtr)0;
        }
 
Top