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

3
我的WinForms应用程序使用Process.Start()打开文件的本机应用程序。我想把屏幕分成两半,一半显示我的WinForms应用程序,另一半显示新进程。我知道我可以使用Process.MainWindowHandle获取窗口句柄,但如何设置其大小和位置呢?
我想我必须使用某种Windows API,但是哪一个以及如何使用呢?由于这并不是我熟悉的领域,我不确定是否需要在64位Windows上使用不同的API(以及如何使用)。
1个回答

6

所涉及的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/zh-cn/library/ms633545.aspx

添加

Process.MainWindowHandle是hWnd参数,您将使用它。 hWndInsertAfter可能是您自己窗体的句柄(Form.Handle)。 您可以使用Screen类型访问有关桌面的信息: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.screen.aspx

Thomas的评论

在调用SetWindowPos之前,请确保等待WaitForInputIdle。

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

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


3
在尝试调整窗口大小之前,您可能需要在进程上调用WaitForInputIdle方法。 - Thomas Levesque
Thomas,这正是我所需要的! - flipdoubt
1
@flipdoubt:Win32只是API的口语化称呼;它的所有功能也适用于64位。官方名称为"Windows API"。 - Billy ONeal
在这里,你可以找到一个使用枚举标志并且带有XML注释的版本:http://www.pinvoke.net/default.aspx/user32.setwindowpos - aybe
最后一个参数uFlags应该设置为什么? - JPProgrammer
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接