Open external application as a child form in C#

Asked

Viewed 1,302 times

1

I am needing to call an external application by 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 be 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);
            }
        } 

1 answer

1


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

Take 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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.