Open external application as a child form in C #

1

I need to call an external application through my current application, in which this external application is being opened outside of my application and I would like it to be opened as a child form of my parent form. This is my current code that is opening this application regardless of the application:

private void buttonBoletos_Click(object sender, EventArgs e)
        {
            try
            {
                string diretorio = Directory.GetCurrentDirectory();
                string caminho = Path.Combine(diretorio, "Boleto.exe");

                Process process = Process.Start(caminho , "GBSODETO");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Aplicativo não encontrado: \"" + ex.Message + "\"", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        } 
    
asked by anonymous 29.10.2015 / 21:34

1 answer

1

Using the Win32 API you can "eat" other software. Basically you open this application and put it "his" parent is the panel you want to work with. If you do not want the "style effect" of MDI you have to adjust the style of the window so that it gets maximized and remove the title bar.

See an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process p = Process.Start("notepad.exe");
            p.WaitForInputIdle(); // Tempo de espera para que a janela do aplicativo "apareça"
            SetParent(p.MainWindowHandle, panel1.Handle); // Aqui está a jogada, colocando o panel "pai"
        }

        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    }
}
    
21.12.2015 / 21:02