Hi everyone !
I'am trying to set up a SimConnect connexion using a WPF application but without success.
I used this topic but it doesn't work : http://forum.avsim.net/topic/224001-simconnect-from-windows-presentation-foundation-wpf/
Here my sample code :
If someone already succeeded to open a SimConnect connexion using a WPF application ?
Thank you in advance.
I'am trying to set up a SimConnect connexion using a WPF application but without success.
I used this topic but it doesn't work : http://forum.avsim.net/topic/224001-simconnect-from-windows-presentation-foundation-wpf/
Here my sample code :
Code:
public partial class MainWindow : Window
{
enum EVENTS { SIMSTART, SIMSTOP, SIMPAUSED, SIMUNPAUSED, POSITIONCHANGED, CRASHED };
// BackgroundWorker Surveillance connexion à SimConnect
private BackgroundWorker workerCheckingSimConnect = new BackgroundWorker();
// Objet SimConnect
public SimConnect gSimConnect = null;
public MainWindow()
{
InitializeComponent();
workerCheckingSimConnect.DoWork += workerCheckingSimConnect_DoWork;
workerCheckingSimConnect.RunWorkerCompleted += workerCheckingSimConnect_RunWorkerCompleted;
// On démarre la surveillance de connexion SimConnect
workerCheckingSimConnect.RunWorkerAsync();
}
private void workerCheckingSimConnect_DoWork(object sender, DoWorkEventArgs e)
{
if (workerCheckingSimConnect.CancellationPending)
{
e.Cancel = true;
return;
}
Boolean lConnecte;
do
{
Thread.Sleep(3000);
try
{
WindowInteropHelper lWih = new WindowInteropHelper(this);
IntPtr lWindowHandle = lWih.Handle;
HwndSource lHwndSource = HwndSource.FromHwnd(lWindowHandle);
lHwndSource.AddHook(new HwndSourceHook(ProcessSimConnectWin32Events));
gSimConnect = new SimConnect(Config.NOM_APPLICATION, lHwndSource.Handle, Config.WM_USER_SIMCONNECT, null, SimConnect.SIMCONNECT_OPEN_CONFIGINDEX_LOCAL);
lConnecte = true;
}
catch
{
lConnecte = false;
}
}
while (!lConnecte);
}
private IntPtr ProcessSimConnectWin32Events(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
handled = false;
if (msg == Config.WM_USER_SIMCONNECT)
{
if (gSimConnect != null)
{
gSimConnect.ReceiveMessage();
handled = true;
}
}
return (IntPtr)0;
}
private void workerCheckingSimConnect_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled)
{
// Associations des événements et des fonctions "CallBack"
gSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
gSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
gSimConnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(SimConnect_OnRecvEvent);
// Association des événements du simulateur
gSimConnect.SubscribeToSystemEvent(EVENTS.SIMSTART, "SimStart");
gSimConnect.SubscribeToSystemEvent(EVENTS.SIMSTOP, "SimStop");
gSimConnect.SubscribeToSystemEvent(EVENTS.SIMPAUSED, "Paused");
gSimConnect.SubscribeToSystemEvent(EVENTS.SIMUNPAUSED, "Unpaused");
gSimConnect.SubscribeToSystemEvent(EVENTS.POSITIONCHANGED, "PositionChanged");
gSimConnect.SubscribeToSystemEvent(EVENTS.CRASHED, "Crashed");
// Activation des événements du simulateur
gSimConnect.SetSystemEventState(EVENTS.SIMSTART, SIMCONNECT_STATE.ON);
gSimConnect.SetSystemEventState(EVENTS.SIMSTOP, SIMCONNECT_STATE.ON);
gSimConnect.SetSystemEventState(EVENTS.SIMPAUSED, SIMCONNECT_STATE.ON);
gSimConnect.SetSystemEventState(EVENTS.SIMUNPAUSED, SIMCONNECT_STATE.ON);
gSimConnect.SetSystemEventState(EVENTS.POSITIONCHANGED, SIMCONNECT_STATE.ON);
gSimConnect.SetSystemEventState(EVENTS.CRASHED, SIMCONNECT_STATE.ON);
// label SimConnectStatus
lbsSimConnectStatus.Content = "OK";
lbsSimConnectStatus.Foreground = new SolidColorBrush(Colors.Gold);
}
}
private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
// ...
}
private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
// ...
}
private void SimConnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data)
{
// ...
}
}
If someone already succeeded to open a SimConnect connexion using a WPF application ?
Thank you in advance.

