windows 如何从 WinForms 应用程序控制新进程窗口的大小和位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3321535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I control the size and position of a new process Window from a WinForms app?
提问by flipdoubt
My WinForms app uses Process.Start()
to open files in their native app. I want to split the screen in half, showing my WinForms app on one half and the new process on the other. I know I can use Process.MainWindowHandle
to get the window handle, but how can I set its size and position?
我的 WinForms 应用程序用于Process.Start()
在其本机应用程序中打开文件。我想将屏幕一分为二,一边显示我的 WinForms 应用程序,另一边显示新进程。我知道我可以Process.MainWindowHandle
用来获取窗口句柄,但是如何设置它的大小和位置?
I imagine I have have to use some kind of Windows API, but which one and how? Since this is not really "in my wheelhouse", I am unsure of whether (and how) I need to use different APIs on 64bit Windows.
我想我必须使用某种 Windows API,但是是哪一种以及如何使用?由于这并不是真正“在我的驾驶室”,我不确定是否(以及如何)需要在 64 位 Windows 上使用不同的 API。
回答by Tergiver
The Windows API method in question is SetWindowPos. You can declare it like so:
有问题的 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);
and read about it here: http://msdn.microsoft.com/en-us/library/ms633545.aspx
并在此处阅读:http: //msdn.microsoft.com/en-us/library/ms633545.aspx
Added
添加
Process.MainWindowHandle is the hWnd parameter you will use. hWndInsertAfter will probably be your own Form's handle (Form.Handle). You can use the Screen type to access information about the desktop: http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx
Process.MainWindowHandle 是您将使用的 hWnd 参数。hWndInsertAfter 可能是您自己的表单句柄 (Form.Handle)。您可以使用屏幕类型访问有关桌面的信息:http: //msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx
Added Thomas' comment
添加了托马斯的评论
Make sure you WaitForInputIdle before calling SetWindowPos.
在调用 SetWindowPos 之前确保您 WaitForInputIdle。
Process process = Process.Start(...);
if (process.WaitForInputIdle(15000))
SetWindowPos(process.MainWindowHandle, this.Handle, ...);
The declaration for SetWindowPos above works for both 32- and 64-bit Windows.
上面的 SetWindowPos 声明适用于 32 位和 64 位 Windows。