1

I'm beginner in developping Wpf i need to know how to run an .exe application in a new TabItem in a TabControl control. i did this snippet:

 private void MenuItem_Click_6(object sender, RoutedEventArgs e) { 
    TabItem nPage = new TabItem();
    nPage.Header = "nouveau";
    TabPage.Items.Add(nPage);
    ProcessStartInfo pInfo = new ProcessStartInfo("Multi-langue.exe");
    pInfo.WorkingDirectory = @"C:\Multi-langue\Multi-langue\bin\Debug";
    Process p = Process.Start(pInfo);
    }

the new application is running but not inside the new TabItem.

So How can i modify my snippet to integrate the display of the second application launched inside the first one?

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 1
    I do not think that you can run external applications inside a `TabItem`. – meilke Sep 25 '13 at 09:13
  • 3
    `TabItem` cannot simply host an external program. WPF is powerful, but not that powerful :) check out [this](http://stackoverflow.com/questions/5028598/hosting-external-app-in-wpf-window) thread – Omri Btian Sep 25 '13 at 09:16
  • http://www.codeproject.com/Articles/23064/Window-Tabifier – Lamloumi Afif Sep 25 '13 at 09:19

1 Answers1

6

i find this solution and it's works fine

 public IntPtr MainWindowHandle { get; set; }
          [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

and

  private void MenuItem_Click_6(object sender, RoutedEventArgs e) { 

        TabItem nPage = new TabItem();
        WindowsFormsHost host = new WindowsFormsHost();
        System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
        host.Child = p;
        nPage.Header = "nouveau";
        nPage.Content = host;
        TabPage.Items.Add(nPage);
        Process proc = Process.Start(
   new ProcessStartInfo()
   {
       FileName = @"C:\Multi-langue\Multi-langue\bin\Debug\Multi-langue.exe",

       WindowStyle = ProcessWindowStyle.Normal
   });
        Thread.Sleep(1000);
        SetParent(proc.MainWindowHandle, p.Handle);

    }
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 3
    Nice! I suggest to put proc.WaitForInputIdle(); instead of the Thread.Sleep(1000) because it is a much faster way to wait for the proc's main UI thread to be ready to receive the SetParent message. – Larry Apr 25 '15 at 22:09