This is a simple technique on how to add an external application on your MDI form as a child. First you have to have MDI form, a button to trigger the event and a panel which will host the embedded application. You have to import first the references.
#VB.NET
Imports System
Imports System.IO
Imports System.Text
Imports System.Diagnostics
Imports System.Drawing
Imports System.Data
Imports System.Windows
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Threading

#CSharp
Using System;
Using System.IO;
Using System.Text;
Using System.Diagnostics;
Using System.Drawing;
Using System.Data;
Using System.Windows;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;
Using System.Threading;

Then after that, you have to declare the necessary functions. 
    <DllImport("User32", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=False)>
    Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As IntPtr
    End Function

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

    Public Shared Function SendMessage(ByVal hWndChild As IntPtr, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
    End Function
The main code for the triggered event is this:
#VB.NET        Dim proc As Process

        proc = Process.Start("notepad.exe")        ' or this proc =     Process.Start("winword.exe")
        'proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        proc.WaitForInputIdle()
        Thread.Sleep(1000)
        SetParent(proc.MainWindowHandle, Me.Panel1.Handle)

        SendMessage(proc.MainWindowHandle, 274, 61488, 0)
        proc.WaitForExit()

#CSharp    
        Process proc

        proc = Process.Start("notepad.exe");        ' or this proc =                                            Process.Start("winword.exe");
        'proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal();
        proc.WaitForInputIdle();
        Thread.Sleep(1000);
        SetParent(proc.MainWindowHandle, Me.Panel1.Handle);

        SendMessage(proc.MainWindowHandle, 274, 61488, 0);
        proc.WaitForExit();
Hope you find it helpful. Dont forget to like, comment and share. Happy coding. Cheers! :D

Dieter
7/15/2014 07:14:23 am

Hi
I tried the code, it starts notepad as a standalone. The Setparent command removes notepad, but doesn't insert it into the panel. What am i doing wrong? Could it be that the values in "SendMessage(proc.MainWindowHandle, 274, 61488, 0);" must be different?
Dieter

Reply
7/15/2014 10:34:15 am

Does the following already included on the code?
This is for vb.net.

<DllImport("User32", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=False)>
Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As IntPtr
End Function

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

Public Shared Function SendMessage(ByVal hWndChild As IntPtr, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
End Function

At first glance, the notepad.exe will appear separately but after a blink of an eye that will be transferred inside the panel.. just a millisecond gap.. please set the windowstate of your main MDI form to maximize since the embeded exe will appear in the screen on its random position..

Reply
Dieter
7/15/2014 12:49:45 pm

Thanks. That worked! Have you tried to position the embedded exe to come up in the same place every time, e.g. setting the left and the top to a specific place?

daniel perez
3/15/2017 06:31:11 am

how do i send keys or message to .exe once it has been embedded?

Reply



Leave a Reply.