DhavalR
DhavalR

Reputation: 1417

MDI parent within another MDI parent form

I am succeeded in opening MDI parent form within MDi parent form by using following method: I made two desktop applications(i.e. App1 and App2) having MDI parent forms as startup. In App1, I have added a panel on MDI parent in which we are going to open the other app i.e. App2. Now I added this code in App1.

using System.Diagnostics;
using System.Runtime.InteropServices;

and

[DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, Int32 lParam);

Now in button click event the following code is used.(App1)

    // Create a new process
        Process proc;

        // Start the process
        proc = Process.Start(Application.StartupPath + @"\App2.exe");
        ////proc = Process.Start("notepad.exe");
        proc.WaitForInputIdle();

        // Set the panel control as the application's parent
        SetParent(proc.MainWindowHandle, this.panel1.Handle);

        // Maximize application
        SendMessage(proc.MainWindowHandle, 274, 61488, 0);
        MessageBox.Show(Application.OpenForms[0].ToString());

Here,Application.StartupPath + @"\App2.exe" is the process or EXE file which I built (Build solution, you know). Firstly,The code works fine when I debug with breakpoint but when I try to run it, the App2 opens as a different process but not in App1. Secondly, I cannot open form which i have added in App2 which is opened as MDI child form (app2).

  Form1 frm = new Form1();
        frm.MdiParent = Application.OpenForms[0];
        frm.Show();

This is how I open child forms in MDI forms.

Upvotes: 0

Views: 2775

Answers (1)

user3733672
user3733672

Reputation: 11

// Create a new process
Process proc;

// Start the process
proc = Process.Start(Application.StartupPath + @"\App2.exe");
proc.WaitForInputIdle();

// Add this by using using System.Threading;
Thread.Sleep(500);

// Set the panel control as the application's parent
SetParent(proc.MainWindowHandle, this.panel1.Handle);

Upvotes: 0

Related Questions