This may be completely impossible, or if it is possible it may involve a lot of p/invoking, but I was just curious if it is.
Can I run an app, that may or may not be coded in C#, inside of another app, as an MDI form?
An example would be running Windows Calculator as a window that appears as an MDI child window in an application.
And of course as is the way with MDI, this would mean that closing the application itself would close any applications providing MDI child windows to it.
As I said, this may be too complex to do without some major API stuff, but if it's possible, any advice?
This is just to make a nice user experience. The app would not have to communicate with the app it's hosting (e.g. no need to get a calculator result). (That may be nice for a future project but as I said it's not imperative.)
fm

1 answers
It's actually quite easy to do this using the API function SetParent().
The only problem I found is that, whilst closing the MDI parent closes the child window, it doesn't actually kill the process. You therefore have to do this manually in, say, the MDI parent's FormClosing eventhandler.
The following code assumes that Form1 is an MDI container:
using System.Diagnostics;
using System.Runtime.InteropServices;
// ....
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public Form1()
{
InitializeComponent();
}
private Process p = null;
private void Form1_Load(object sender, EventArgs e)
{
p = new Process();
p.StartInfo.FileName = "calc.exe";
p.Start();
p.WaitForInputIdle();
SetParent(p.MainWindowHandle, this.Handle);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!p.HasExited) p.CloseMainWindow();
}
}
answered 2 years ago by:
17279
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!