0
我成功地在MDI父窗體窗體中使用以下方法打開MDI父窗體: 我製作了兩個桌面應用程序(即App1和App2),它們具有MDI父窗體作爲啓動。 在App1中,我在MDI父項中添加了一個面板,我們將在其中打開其他應用,即App2。 現在我在App1中添加了這段代碼。在另一個MDI父窗體中的MDI父窗體
using System.Diagnostics; using System.Runtime.InteropServices;
和
[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);
現在,在按鈕的單擊事件下面的代碼被使用。(應用1)
// 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());
這裏,Application.StartupPath + @「\ App2.exe」是我構建的進程或EXE文件(您知道生成解決方案)。首先,當我用斷點調試時,代碼工作正常,但當我嘗試運行它時,App2以不同的過程打開,但不在App1中打開。其次,我無法打開我在App2中添加的窗體,該窗體以MDI子窗體(app2)的形式打開。
Form1 frm = new Form1();
frm.MdiParent = Application.OpenForms[0];
frm.Show();
這是我如何在MDI窗體中打開子窗體。
由SetParent()支持,因爲這在Windows版本3中是可能的。一個尚未具有進程和線程的Windows版本。這個好的結果與你的程序像Windows 3.x程序成反比。 20年後,這些機率變得很低。完全沒有錯誤檢查,否則也不可能診斷問題。使用SetLastError = true並在winapi函數返回失敗代碼時引發Win32Exception是必需的。 –
所以我應該改變什麼(bcoz我無法理解你的評論totaly。)?其次,我嘗試用相同的方法打開notepad.exe。有效。 – DhavalR
@hans Passnat:我將我的代碼更改爲http://stackoverflow.com/questions/10102526/c-sharp-mainwindowhandle-always-zero,我能夠達到我想要的。但我仍然很想知道你的評論(只是爲了知識)。 – DhavalR