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

fli*_*ubt 3 c# windows 32bit-64bit winforms

我的WinForms应用程序用于Process.Start()在其本机应用程序中打开文件.我想将屏幕分成两半,在一半显示我的WinForms应用程序,在另一半显示新进程.我知道我可以用它Process.MainWindowHandle来获取窗口句柄,但是如何设置其大小和位置?

我想我必须使用某种Windows API,但是哪一个以及如何使用?由于这不是"我的驾驶室",我不确定是否(以及如何)我需要在64位Windows上使用不同的API.

Ter*_*ver 5

有问题的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);
Run Code Online (Sandbox Code Playgroud)

并在此处阅读: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, ...);
Run Code Online (Sandbox Code Playgroud)

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

  • 在尝试调整窗口大小之前,您可能希望在进程上调用WaitForInputIdle ... (3认同)