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

前端 未结 1 1472
孤城傲影
孤城傲影 2020-12-16 08:03

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 proc

相关标签:
1条回答
  • 2020-12-16 08:34

    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.

    0 讨论(0)
提交回复
热议问题