0

I would like to create a C# console application to launch multiple instances of IE maximized on multiple monitors.

Update: Here is what I have tried so far. When I launch the second IE instance, it does not open on the second screen. I think it has something to do with the MainWindowHandle since IE may have only one main window handle that it shares with multiple windows. The last line of code actually throws an InvalidOperationException. This code works for launching notepad, but not for IE.

using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestLauncher
{
    class Launcher2
    {
        [DllImport("user32.dll")]
        private extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
        const int SWP_SHOWWINDOW = 0x0040;

        static readonly IntPtr HWND_TOP = IntPtr.Zero;

        public void Launch()
        {
            Process p1 = new Process();
            p1.StartInfo.FileName = "iexplore.exe";
            p1.StartInfo.Arguments = "microsoft.com";
            p1.Start();
            p1.WaitForInputIdle(2000);
            System.Threading.Thread.Sleep(2000);

            Rectangle monitor = Screen.AllScreens[0].WorkingArea;
            SetWindowPos(p1.MainWindowHandle, HWND_TOP, monitor.Left, monitor.Top, monitor.Width, monitor.Height, SWP_SHOWWINDOW);


            Process p2 = new Process();
            p2.StartInfo.FileName = "iexplore.exe";
            p2.StartInfo.Arguments = "google.com";
            p2.Start();
            p2.WaitForInputIdle(2000);
            System.Threading.Thread.Sleep(2000);

            Rectangle monitor2 = Screen.AllScreens[1].WorkingArea;
            SetWindowPos(p2.MainWindowHandle, HWND_TOP, monitor2.Left, monitor2.Top, monitor2.Width, monitor2.Height, SWP_SHOWWINDOW);
        }
    }
}

If possible, I am seeking a solution that is flexible enough to launch other applications besides IE also.

Dave Johnson
  • 782
  • 1
  • 7
  • 17
  • Can you show what you've tried so far? – M.Babcock Feb 10 '12 at 01:34
  • Does it have to be some specific program that you are launching? Is it one that you control the source code for? Or could it be, say, notepad? – Chris Shain Feb 10 '12 at 01:44
  • @M.Babcock - I updated my post with what I have tried so far – Dave Johnson Feb 10 '12 at 01:49
  • @ChrisShain I will most likely be launching instances of IE with specific URLs, but it could be other programs. I will not have control of the source code of the launched programs. – Dave Johnson Feb 10 '12 at 01:50
  • 1
    possible duplicate with good description of the process: http://stackoverflow.com/questions/3750113/c-sharp-launch-an-application-and-send-it-to-second-monitor – M.Babcock Feb 10 '12 at 01:54
  • @M.Babcock The accepted answer at http://stackoverflow.com/questions/3750113/c-sharp-launch-an-application-and-send-it-to-second-monitor seems like a reasonable approach, but I am not adept enough with the Windows API to implement it in C# without some more specific source code. – Dave Johnson Feb 10 '12 at 02:00
  • You only maximized the window, you didn't move it. – Hans Passant Feb 10 '12 at 02:11
  • @HansPassant Yes, that is correct. I'm seeking code example to move application to a specific monitor and maximize. – Dave Johnson Feb 10 '12 at 02:28
  • 1
    @DaveJohnson If you give it a try, we can always guide you along when you get stuck. – Zenexer Feb 10 '12 at 04:08
  • What about hosting IE control on a form, and maximizing that form from code? – Daniel Mošmondor Feb 12 '12 at 00:42
  • @DanielMošmondor, thanks for the good idea. I am hoping for a solution that is flexible enough to launch other applications besides IE if possible, but I may not be able to achieve this and your idea is good as an alternative solution. – Dave Johnson Feb 14 '12 at 20:04

0 Answers0