Here's a working example that shows how to launch one C# exe from another, and position the two Forms (the first app's Main Form, the second app's Main Form) so they each occupy half the working-screen-area of the primary monitor.
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace YourApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
[Flags]
public enum SetWindowPosFlags
{
NOSIZE = 0x0001,
NOMOVE = 0x0002,
NOZORDER = 0x0004,
NOREDRAW = 0x0008,
NOACTIVATE = 0x0010,
DRAWFRAME = 0x0020,
FRAMECHANGED = 0x0020,
SHOWWINDOW = 0x0040,
HIDEWINDOW = 0x0080,
NOCOPYBITS = 0x0100,
NOOWNERZORDER = 0x0200,
NOREPOSITION = 0x0200,
NOSENDCHANGING = 0x0400,
DEFERERASE = 0x2000,
ASYNCWINDOWPOS = 0x4000
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private string app2path = @"your complete second app exe filepath";
public Rectangle ScreenRect;
public ProcessStartInfo psi;
public Process secondAppProcess;
private void Form1_Load(object sender, EventArgs e)
{
ScreenRect = Screen.PrimaryScreen.WorkingArea;
this.Size = new Size(ScreenRect.Width/2, ScreenRect.Height);
this.Location = ScreenRect.Location;
psi = new ProcessStartInfo(app2path);
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
secondAppProcess = new Process();
secondAppProcess.StartInfo = psi;
secondAppProcess.Start();
Thread.Sleep(100);
secondAppProcess.WaitForInputIdle();
SetWindowPos(
secondAppProcess.MainWindowHandle,
HWND_TOP,
this.Left + this.Width,
this.Top,
this.Width,
this.Height,
SetWindowPosFlags.SHOWWINDOW);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
secondAppProcess.CloseMainWindow();
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
MoveWindow(secondAppProcess.MainWindowHandle, this.Right, this.Top, this.Width, this.Height, true);
}
}
}
Notes:
1. in this example I chose to close the second app when the first app closes ... that doesn't mean you have to do that, but ... think about the issue.
2. in this example, when the firs app main window moves the second app main window moves along with it ... just an example to illustrate the use of the 'MoveWindow API call. You just as easily deal with any change in the first app's window to set the size, or whatever, for the second app's window.