0
votes

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.

1
This is supported by SetParent() because this was possible in Windows version 3. A version of Windows that didn't yet have processes and threads. The odds this still comes to a good end is inversely proportional to your program acting like a Windows 3.x program. After 20 years, those odds get low. The complete lack of error checking otherwise makes diagnosing problems impossible as well. Using SetLastError = true and throwing Win32Exception when the winapi function returned a failure code is required.Hans Passant
So what changes i should make (bcoz I could not understand your comment totaly.)? Second I tried to open notepad.exe with the same method. It worked.DhavalR
@hans Passnat: I changed my code to stackoverflow.com/questions/10102526/… and I am able to what I want. But I am still curious to know about your comment (just for knowledge.).DhavalR
One more thing still I can not open forms in MDIParent2 which is now opend in panel (in app1.).DhavalR

1 Answers

0
votes
// 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);