简体   繁体   English

C# 将窗口移动到辅助屏幕

[英]C# move a window to secondary screen

Hello fellow developers各位开发者好

I have a little question.我有一个小问题。 I have a problem that's starting to annoy me.我有一个开始让我烦恼的问题。

I have written a program in C# which opens the Chrome Browser.我用 C# 编写了一个程序来打开 Chrome 浏览器。 However, the Chrome Browser always opens at the place where it was last opened.但是,Chrome 浏览器始终会在上次打开的位置打开。 Is there a possibility to put the browser window directly on the second screen at every start?是否可以在每次启动时将浏览器窗口直接放在第二个屏幕上? My goal is that the Chrome Browser always starts automatically on the other screen.我的目标是 Chrome 浏览器总是在另一个屏幕上自动启动。

Anybody got an idea?有人有想法吗?

Thank you谢谢

You could try the following:您可以尝试以下操作:

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);

EDIT:编辑:

Just to explain further, if you have 3 screens with a resolution of 1920x1080 setting the x parameter in the MoveWindow() method will place the window at the left most place of the first screen, setting the x parameter to 1920 will place the application on the second screen, setting the x parameter to 3840 the process would be on the third screen.进一步解释一下,如果您有 3 个分辨率为1920x1080屏幕,在MoveWindow()方法中设置x参数会将窗口放置在第一个屏幕的最左侧,将x参数设置为1920会将应用程序放置在第二个屏幕,将x参数设置为3840过程将在第三个屏幕上。 With having access to all the screens and their widths, you should be able to accurately place the process on the screen of choice almost all the time unless a user has custom ordering of their multi screen layout, then I'm not 100% sure if this would be ideal.通过访问所有屏幕及其宽度,除非用户对其多屏幕布局进行自定义排序,否则您应该几乎始终能够准确地将流程放置在选择的屏幕上,那么我不能 100% 确定是否这将是理想的。

EDIT 2:编辑2:

The above code will not work on .NetCore 2.2 and lower as it uses System.Windows.Forms which I believe will only be introduced in .NetCore 3.0上述代码不适用于.NetCore 2.2及更低版本,因为它使用System.Windows.Forms ,我相信它只会在.NetCore 3.0引入

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM