我怎样才能控制从WinForms应用程序的大小,以及新工艺窗口的位置? [英] How can I control the size and position of a new process Window from a WinForms app?
问题描述
我的WinForms应用程序使用的Process.Start()
在其本机应用程序打开的文件。欲在屏幕劈成两半,示出在一个一半和另一新的过程我WinForms应用程序。我知道我可以使用 Process.MainWindowHandle
来获取窗口句柄,但我怎么可以设置它的大小和位置?
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?
我想我必须要使用某种类型的Windows API的,但哪一个又如何呢?由于这是不是真的在我的舵手,我不确定我是否(以及如何)需要使用在64位的Windows不同的API。
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的方法是SetWindowPos。你可以这样声明:
The Windows API method in question is SetWindowPos. You can declare it like so:
[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.Handle)。
你可以使用屏幕类型访问有关桌面信息:
的 http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx
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
新增托马斯的评论
请确保您WaitForInputIdle调用SetWindowPos之前。
Make sure you WaitForInputIdle before calling SetWindowPos.
Process process = Process.Start(...);
if (process.WaitForInputIdle(15000))
SetWindowPos(process.MainWindowHandle, this.Handle, ...);
该声明上述两部作品SetWindowPos 32位和64位Windows。
The declaration for SetWindowPos above works for both 32- and 64-bit Windows.
这篇关于我怎样才能控制从WinForms应用程序的大小,以及新工艺窗口的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!