我试图在一个Visual C# 2010 - Windows Forms应用程序中启动一个外部进程。目标是将该进程作为隐藏窗口启动,并在稍后的时间将窗口显示出来。
我已经更新了我的进展:
//Initialization
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hwnd, bool enable);
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr handle, int x, int y, int width,
int height, bool redraw);
SW_SHOW = 5;
以下代码被放置在我的主函数中:
ProcessStartInfo info = new ProcessStartInfo("process.exe");
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForInputIdle();
IntPtr HWND = p.MainWindowHandle;
System.Threading.Thread.Sleep(1000);
ShowWindow(HWND, SW_SHOW);
EnableWindow(HWND, true);
MoveWindow(HWND, 0, 0, 640, 480, true);
然而,由于窗口是以“隐藏”的方式启动的,
p.MainWindowHandle = 0
。我无法成功地显示窗口。我还尝试了HWND = p.Handle
,但没有成功。有没有办法为我的窗口提供一个新的句柄?这可能会解决我的问题。
参考资料: MSDN ShowWindow MSDN论坛 如何导入.dll文件
ProcessStartInfo info = new ProcessStartInfo("iexplore");
- Matt Barr