当我以MDI Child打开外部应用程序时,应用程序将打开,但不会作为MDI子项打开。我有一个正确运行notepad.exe的类,但它不适用于我自己的应用程序:
public class ExternalApp {
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern uint SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public static void LoadProcessInControl(string app_path, Control parent) {
System.Diagnostics.Process p = System.Diagnostics.Process.Start(app_path);
p.WaitForInputIdle();
ExternalApp.SetParent(p.MainWindowHandle, parent.Handle);
}
}
我用这个函数调用函数:
ExternalApp.LoadProcessInControl(@"C:\Users\Bálint\Documents\Visual Studio 2013\Projects\TesztApp\TesztApp.exe", this);
有什么问题?
答案 0 :(得分:2)
实际上我在评论中误解了那些不需要设置的属性。已经有一段时间了。 :)为了做到这一点,我在儿童表格中使用了一个小组取得了一些成功:
public partial class Form2 : Form
{
[DllImport("user32.dll", SetLastError = true)]
private 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);
public Form2()
{
InitializeComponent();
Process process = new Process();
process.StartInfo.FileName = "Notepad.exe";
process.Start();
process.WaitForInputIdle();
SetParent(process.MainWindowHandle, panel1.Handle);
//This maximizes the process window.
SendMessage(process.MainWindowHandle, 274, 61488, 0);
return;
}
}
SendMessage的文档是here。 子窗体如下所示,嵌入了记事本:
public Form1()
{
InitializeComponent();
Form2 newForm2 = new Form2();
newForm2.MdiParent = this;
newForm2.Show();
}