Bonjour,

Nous sommes en train de refaire une application C, en WPF/C#.
Cette application utilise une autre appli en c++/MFC que nous n'avons pas le temps de reprendre pour le moment. Nous cherchons donc à l'intégrer au mieux depuis notre appli wpf.

Nous avons utilisé la technique de "HwndHost" mais nous avons qq soucis :
1/ on relance l'application à chaque fois qu'on en a besoin
2/ on perd le focus dans les textbox de l'appli c++
3/ il y a un drôle d'effet à l'ouverture car elle s'ouvre en plein écran puis se redimensionne à la taille que nous lui donnons.

Et nous avons des objectifs d'intégration que nous n'arrivons pas à réaliser :
1/ communiquer avec cette appli pour lancer nos fenêtres de dialogues, pour savoir comment l'utilisateur à quitter l'appli c++ (en fermant, en demandant un autre environnement... )
2/ garder l'appli ouverte, la "minimiser" quand on en n'a pas besoin, lui envoyer des messages.

Qq précisions c'est une appli MDI avec un ruban

Aurore

un bout du code que nous utilisons :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
public class EurekaHwndHost : HwndHost
    {
        int hostHeight, hostWidth;
        EurekaEnvironment oContext;
 
        public EurekaHwndHost(double height, double width, EurekaEnvironment oenv)
        {
            hostHeight = (int)height;
            hostWidth = (int)width;
            oContext = oenv;
        }
 
 
        public IntPtr hwndHost;
        private IntPtr hookId = new IntPtr(3);
        private NativeMethods.HookProc hookProc;
        Process procEureka = null;
        protected override HandleRef BuildWindowCore(HandleRef hwndParent)
        {
if (procEureka == null)
            {
                string sCommand = System.IO.Path.Combine(MyTools.GetEurekaDirectoryPath(), "eurekav6.exe");
 
                procEureka = new Process();
                procEureka.StartInfo = new ProcessStartInfo(sCommand, oContext.EurParams.GetExeCommandArgs());
                procEureka.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                procEureka.EnableRaisingEvents = true;
                procEureka.Exited += new EventHandler(procEureka_Exited);
                procEureka.Start();               
 
                 try
                {
                    hwndHost = procEureka.MainWindowHandle;
                }
                catch (Exception)
                {
                    return new HandleRef(this, IntPtr.Zero);
                }
                int iTry = 0;
                while (hwndHost.Equals(IntPtr.Zero) && iTry < 5)
                {
                    System.Threading.Thread.Sleep(2000);
                    Process[] processes = Process.GetProcessesByName("EurekaV6");
                    foreach (Process pnn in processes)
                    {
                        hwndHost = pnn.MainWindowHandle;
                    }
                    iTry++;
                }
                if (hwndHost.Equals(IntPtr.Zero))
                {
                     return new HandleRef(this, IntPtr.Zero);
                }
 
                uint newStyle = NativeMethods.WS_VISIBLE + NativeMethods.WS_MAXIMIZE + NativeMethods.WS_DLGFRAME + NativeMethods.WS_CHILD + NativeMethods.WS_SYSMENU; // oldStyle & ~NativeMethods.WS_BORDER & ~NativeMethods.WS_SIZEBOX | NativeMethods.WS_CHILD & ~NativeMethods.WS_DISABLED;
                NativeMethods.SetWindowLong(hwndHost, NativeMethods.GWL_STYLE, newStyle);
                NativeMethods.SetParent(hwndHost, hwndParent.Handle);
 
                IntPtr hMenu = NativeMethods.GetSystemMenu(hwndHost, false);
                NativeMethods.RemoveMenu(hMenu, NativeMethods.SC_MOVE, NativeMethods.MF_BYCOMMAND);
                NativeMethods.RemoveMenu(hMenu, NativeMethods.SC_SIZE, NativeMethods.MF_BYCOMMAND);
                NativeMethods.RemoveMenu(hMenu, NativeMethods.SC_MAXIMIZE, NativeMethods.MF_BYCOMMAND);
                NativeMethods.RemoveMenu(hMenu, NativeMethods.SC_MINIMIZE, NativeMethods.MF_BYCOMMAND);
                NativeMethods.RemoveMenu(hMenu, NativeMethods.SC_RESTORE, NativeMethods.MF_BYCOMMAND);
                NativeMethods.DrawMenuBar(hwndHost);
 
                hookProc = new NativeMethods.HookProc(MyHookHandler);
                NativeMethods.SetWindowsHookEx(hookId.ToInt32(), hookProc, IntPtr.Zero,
                    System.Threading.Thread.CurrentThread.ManagedThreadId);
            }
 
            return new HandleRef(this, hwndHost);
        }
 
        delegate void dlgProcEurekaExited(object sender, EventArgs e);
        void procEureka_Exited(object sender, EventArgs e)
        {
            oContext.procEureka_Exited(sender,e);
        }
 
        private int MyHookHandler(int nCode, IntPtr wParam, IntPtr lParam)
        {
            return NativeMethods.CallNextHookEx(hookId.ToInt32(), nCode, wParam, lParam);
        }
 
        protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            handled = false;
            return IntPtr.Zero;
        }
        protected override void DestroyWindowCore(HandleRef hwnd)
        {
            procEureka.Exited -= new EventHandler(procEureka_Exited);
            NativeMethods.DestroyWindow(hwnd.Handle);
            procEureka.Kill();
        }
    }