在Winform Panel中打开C#exe

时间:2014-06-09 07:45:54

标签: c#

我只是使用C#代码编写一个exe,我想在C#winform中使用SetParent运行exe

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

1 个答案:

答案 0 :(得分:2)

你可以尝试这个(菜单文件名没有.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