C#将窗口移到辅助屏幕

C#将窗口移到辅助屏幕,c#,google-chrome,browser,C#,Google Chrome,Browser,各位开发者好 我有个小问题。我有个问题开始困扰我了 我用C语言编写了一个程序,可以打开Chrome浏览器。 但是,Chrome浏览器总是在上次打开的位置打开。 是否有可能在每次启动时将浏览器窗口直接放在第二个屏幕上? 我的目标是Chrome浏览器总是在另一个屏幕上自动启动 有人有主意吗 谢谢您可以尝试以下方法: using System; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServic

各位开发者好

我有个小问题。我有个问题开始困扰我了

我用C语言编写了一个程序,可以打开Chrome浏览器。 但是,Chrome浏览器总是在上次打开的位置打开。 是否有可能在每次启动时将浏览器窗口直接放在第二个屏幕上? 我的目标是Chrome浏览器总是在另一个屏幕上自动启动

有人有主意吗


谢谢

您可以尝试以下方法:

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

public void StartChrome()
{
    var allScreens = Screen.AllScreens.ToList();

    var screenOfChoice = allScreens[1]; // repllace with your own logic

    var chromeProcess = new Process
    {
            StartInfo =
            {
                    Arguments = "https://www.google.com --new-window --start-fullscreen",
                    FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
                    WindowStyle = ProcessWindowStyle.Normal
            }
    };

    chromeProcess.Start();

    // Needed to move the the process.
    Thread.Sleep(1000);

    // setting the x value here can help you determmine which screen to move the process to
    // 0 will be the first screen, and the '.WorkingArea.Right' value to the previous screen's '.WorkingArea.Right' would change which 
    // screen to display it on.
    MoveWindow(chromeProcess.MainWindowHandle, screenOfChoice.WorkingArea.Right, screenOfChoice.WorkingArea.Top, screenOfChoice.WorkingArea.Width, screenOfChoice.WorkingArea.Height, false);
}

[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
编辑:

为了进一步解释,如果您有3个分辨率为
1920x1080
的屏幕,在
MoveWindow()方法中设置
x
参数将使窗口位于第一个屏幕的最左侧,将
x
参数设置为
1920
将使应用程序位于第二个屏幕上,将
x
参数设置为
3840
过程将出现在第三个屏幕上。通过访问所有屏幕及其宽度,您应该能够几乎一直准确地将流程放置在所选屏幕上,除非用户对其多屏幕布局进行自定义排序,否则我不能100%确定这是否理想

编辑2:


上述代码不适用于
.NetCore 2.2
及更低版本,因为它使用的是
系统.Windows.Forms
,我相信这些代码只会在
中介绍。NetCore 3.0

您可以发布一些关于如何打开浏览器的示例代码吗?