简体   繁体   English

如何移动第二个打开的浏览器 window?

[英]How can I move a second opened browser window?

I'm trying to make a program that opens a browser on each multi-display.我正在尝试制作一个在每个多显示器上打开浏览器的程序。 When I did it with a notepad, it worked.当我用记事本做的时候,它起作用了。 However, when it's a browser didn't work and showed the error "System.InvalidOperationException: Process must exit before requested information can be determined".但是,当它是浏览器无法工作并显示错误“System.InvalidOperationException:进程必须退出才能确定请求的信息”时。 I will appreciate your help with this situation.感谢您对这种情况的帮助。

This is my code:这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern int MoveWindow(IntPtr hwnd, int x, int y,
    int nWidth, int nHeight, int bRepaint);

        private void Form1_Load(object sender, EventArgs e)
        {

            foreach (Screen item in Screen.AllScreens)
            {
                //Open a web browser
                System.Diagnostics.Process process1 = System.Diagnostics.Process.Start("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
                //System.Diagnostics.Process process2 = System.Diagnostics.Process.Start("notepad.exe");
                process1.WaitForInputIdle();
                //process2.WaitForInputIdle();

                //Move the browser window
                MoveWindow(process1.MainWindowHandle, item.Bounds.X, item.Bounds.Y, item.Bounds.Width, item.Bounds.Height, 1);
                //MoveWindow(process2.MainWindowHandle, item.Bounds.X, item.Bounds.Y, item.Bounds.Width, item.Bounds.Height, 1);

            }
        }
    }
}

It seems that msedge.exe use a host process to start different tabs, so we can't use the reture process ID to handle it.好像msedge.exe使用了一个宿主进程来启动不同的标签页,所以我们不能使用reture process ID来处理它。

Compare created process ID with processes in the Task Manager, the process 38756 is missing.将创建的进程 ID 与任务管理器中的进程进行比较,缺少进程38756

在此处输入图像描述

Another tool to browse edge relative processes is the built-in task manager of edge .浏览edge相关进程的另一个工具是edge的内置任务管理器。

在此处输入图像描述

在此处输入图像描述

So re-search the target edge process is necessary.所以重新搜索目标edge的过程是必要的。

This repo https://github.com/alex-tomin/Tomin.Tools.KioskMode demostrate how to move chrome window into different monitors and set as full screen.这个 repo https://github.com/alex-tomin/Tomin.Tools.KioskMode演示了如何将 chrome window 移动到不同的显示器并设置为全屏。

I tried to extract the necessary and modify your code like below, it works well on my Windows.我尝试提取必要的并修改您的代码,如下所示,它在我的 Windows 上运行良好。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestWinFormsApp
{
    // Get full definition of SetWindowPosFlags here:
    // https://www.pinvoke.net/default.aspx/Enums/SetWindowPosFlags.html
    [Flags]
    public enum SetWindowPosFlags : uint
    {
        SWP_NOREDRAW = 0x0008,
        SWP_NOZORDER = 0x0004
    }

    // Get full definition of ShowWindowCommands here:
    // https://www.pinvoke.net/default.aspx/Enums/ShowWindowCommand.html
    public enum ShowWindowCommands
    {
        Maximize = 3,
        Restore = 9,
    }

    public static class WinApi
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            var ignoreHandles = new List<IntPtr>();
            foreach (Screen item in Screen.AllScreens)
            {
                this.StartEdgeProcess();

                var windowHandle = this.GetWindowHandle("msedge", ignoreHandles);
                ignoreHandles.Add(windowHandle);

                WinApi.ShowWindow(windowHandle, ShowWindowCommands.Restore);
                WinApi.SetWindowPos(windowHandle, IntPtr.Zero, item.Bounds.Left, item.Bounds.Top, 800, 600, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOREDRAW);
                WinApi.ShowWindow(windowHandle, ShowWindowCommands.Maximize);
            }
        }

        private void StartEdgeProcess()
        {
            var process = new Process();
            process.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe";
            process.StartInfo.Arguments = "about:blank" + " --new-window -inprivate ";
            process.Start();
            process.WaitForInputIdle();
        }

        private IntPtr GetWindowHandle(string processName, List<IntPtr> ignoreHandlers)
        {
            IntPtr? windowHandle = null;
            while (windowHandle == null)
            {
                windowHandle = Process.GetProcesses()
                    .FirstOrDefault(process => process.ProcessName == processName
                        && process.MainWindowHandle != IntPtr.Zero
                        && !string.IsNullOrWhiteSpace(process.MainWindowTitle)
                        && !ignoreHandlers.Contains(process.MainWindowHandle))
                    ?.MainWindowHandle;
            }

            return windowHandle.Value;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得在浏览器中打开的所有选项卡的URL? - How can I get the url of all tabs opened in a browser? 使用 WinAppDriver 和 c# 如何使用“sendkeys”或在新打开的浏览器窗口中查找新元素并返回到我的旧窗口? - Using WinAppDriver and c# how can I use "sendkeys" or find new elements in a new opened browser window and return back to my old window? 如何将十进制值从调用者窗口发送到单击事件中将被调用/打开的窗口 - How can I send decimal value from caller window to window that will be called/opened on click event 如何使用WinApi移动窗口 - How Can I Move The Window Like I Want With WinApi 打开子窗口时如何查看浏览器的后退按钮? - How to check browser back button is clicked when child window is opened? 如何知道 WPF 窗口是否打开 - How do I know if a WPF window is opened 如果我已在浏览器中成功打开新选项卡,如何从控制器向屏幕写入信息? - If I have successfully opened a new tab in a browser, how can I write information to the screen from the controller? 如何关闭当前浏览器窗口并重定向到以前打开的同一浏览器窗口 - How to close current Browser window and redirect to a previously opened window for the same browser 如果我移动错误消息,如何移动Form2的窗口? - How I can move Window of Form2 if I move the error message? 从.Net应用程序打开时,如何保持Python脚本命令窗口打开? - How can I keep a Python script command window open when opened from a .Net application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM