How can I control the size and position of a new process Window from a WinForms app?

时光总嘲笑我的痴心妄想 提交于 2019-12-29 05:35:12

问题


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?

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.


回答1:


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);

and read about it here: 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

Added Thomas' comment

Make sure you WaitForInputIdle before calling SetWindowPos.

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.



来源:https://stackoverflow.com/questions/3321535/how-can-i-control-the-size-and-position-of-a-new-process-window-from-a-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!