2014-06-09 124 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