How can I control the size and position of a new process window from a WinForms application?

My WinForms application uses Process.Start()to open files in its own application. I want to split the screen in half, showing my WinForms application in one half and the new one in the other. I know what I can use Process.MainWindowHandleto get a window handle, but how can I set its size and position?

I assume I need to use some kind of Windows API, but which one and how? Since this is not really “in my wheelhouse”, I'm not sure what (and how) I need to use different APIs on 64-bit Windows.

+3
source share
1 answer

SetWindowPos is used as the Windows API method. You can declare it like this:

[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 that you will use. hWndInsertAfter will probably be your own form handle (Form.Handle). You can use a screen type to access desktop information: http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

Thomas comment added

Before calling SetWindowPos, make sure you WaitForInputIdle.

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

The declaration for SetWindowPos above works for both 32-bit and 64-bit Windows.

+5
source

Source: https://habr.com/ru/post/1756259/


All Articles