如何从WinForms应用程序控制新进程窗口的大小和位置?

我的WinForms应用程序使用Process.Start()在其本机应用程序中打开文件。 我想把屏幕分成两半,一半显示我的WinForms应用程序,另一半显示新的程序。 我知道我可以使用Process.MainWindowHandle来获得窗口句柄,但我怎样才能设置它的大小和位置?

我想我必须使用某种Windows API,但是哪一个,以及如何? 由于这不是真的“在我的舵手”,我不确定是否(以及如何)在64位Windows上使用不同的API。

有问题的Windows API方法是SetWindowPos。 你可以这样声明:

 [DllImport("user32.dll")] private extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); 

并在这里阅读: http : //msdn.microsoft.com/en-us/library/ms633545.aspx

添加

Process.MainWindowHandle是您将使用的hWnd参数。 hWndInsertAfter可能是你自己的Form的句柄(Form.Handle)。 您可以使用屏幕类型来访问有关桌面的信息: http : //msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

添加托马斯的评论

在调用SetWindowPos之前确保WaitForInputIdle。

 Process process = Process.Start(...); if (process.WaitForInputIdle(15000)) SetWindowPos(process.MainWindowHandle, this.Handle, ...); 

上面的SetWindowPos声明适用于32位和64位Windows。