2014-06-09 123 views
-2

我只是用C#代碼寫一個exe,我想使用的setparent在C#中的winformWinform的面板中打開C#EXE

Process proc = Process.Start(
     new ProcessStartInfo() 
     { 
      FileName = "Menu", 
      Arguments = "/c echo hello user ^<!^> && pause", 
      WindowStyle = ProcessWindowStyle.Minimized 
     }); 
     SetParent(proc.MainWindowHandle, this.panel2.Handle); 
+0

您正在嘗試設置時光倒流到1990年,回來的時候,這不是一個問題。你試圖更好地運行的程序是90年代的程序。就像記事本一樣,先用那個試一下。非常明顯的錯誤是試圖在面板中放置一個最小化窗口,而不是等待進程創建其主窗口句柄。調用Process.WaitForInputIdle()是最低要求。 –

回答

2

你可以試試這個(菜單文件名不具有擴展名爲.exe來運行exe )

[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 
在功能

然後

var proc = new Process(); 
proc.StartInfo.FileName = "Menu.exe"; 
proc.StartInfo.Arguments = "/c echo hello user ^<!^> && pause", 
proc.Start(); 
SetParent(proc.MainWindowHandle, this.panel2.Handle); 

更新

using System.Runtime.InteropServices; 
using System.Threading; 
[DllImport("user32.dll", SetLastError = true)] 
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); 
[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

然後

var proc = new Process(); 
proc.StartInfo.FileName = "Menu.exe"; 

proc.Start(); 

IntPtr ptr = IntPtr.Zero; 
while ((ptr = proc.MainWindowHandle) == IntPtr.Zero) ; 
SetParent(proc.MainWindowHandle, trackerPanel.Handle); 
MoveWindow(proc.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true); 

參考this

+0

這個過程不起作用 – user1301802

+0

你得到了什麼錯誤?過程是否開始?嘗試用空的參數,讓我知道 – faby

+0

也不工作項目開始面板 – user1301802