我正在尝试在 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
没有成功。
有没有办法为我的窗口提供一个新的句柄?这可能会解决我的问题。
参考: