0

我想创建一个 C# 控制台应用程序来启动在多个监视器上最大化的多个 IE 实例。

更新:这是我到目前为止所尝试的。当我启动第二个 IE 实例时,它不会在第二个屏幕上打开。我认为它与 MainWindowHandle 有关,因为 IE 可能只有一个与多个窗口共享的主窗口句柄。最后一行代码实际上抛出了一个 InvalidOperationException。此代码适用于启动记事本,但不适用于 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);
        }
    }
}

如果可能的话,我正在寻找一个足够灵活的解决方案来启动除 IE 之外的其他应用程序。

4

0 回答 0