C# 在辅助监视器上显示 Windows 窗体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1363374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:40:47  来源:igfitidea点击:

Showing a Windows form on a secondary monitor?

c#winformsmultiple-monitorssetbounds

提问by David Hodgson

I'm trying to set a Windows Form on secondary monitor, as follows:

我正在尝试在辅助监视器上设置 Windows 窗体,如下所示:

private void button1_Click(object sender, EventArgs e)
{
    MatrixView n = new MatrixView();
    Screen[] screens = Screen.AllScreens;
    setFormLocation(n, screens[1]);
    n.Show();
}

private void setFormLocation(Form form, Screen screen)
{
    // first method
    Rectangle bounds = screen.Bounds;
    form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);

    // second method
    //Point location = screen.Bounds.Location;
    //Size size = screen.Bounds.Size;

    //form.Left = location.X;
    //form.Top = location.Y;
    //form.Width = size.Width;
    //form.Height = size.Height;
}

The properties of bounds seem correct, but in both methods I've tried, this maximizes the form on the primary monitor. Any ideas?

边界的属性似乎是正确的,但在我尝试过的两种方法中,这都最大化了主监视器上的表单。有任何想法吗?

采纳答案by Sesh

Try setting StartPosition parameter as FormStartPosition.Manualinside your SetFormLocationmethod.

尝试FormStartPosition.Manual在您的SetFormLocation方法中设置 StartPosition 参数。

回答by Henk Holterman

Are you sure screens[1]is your secondary? Give screens[0]a try. Your code is basically correct.

你确定screens[1]是你的次要?给screens[0]一试。你的代码基本上是正确的。



Ok, I checked, you will have to do it after the Show():

好的,我检查过,您必须在 Show() 之后执行此操作:

n.Show();
setFormLocation(n, screens[1]);

which gives some unwanted flicker. But you can probably do:

这会产生一些不需要的闪烁。但你可能可以这样做:

n.SetBounds(-100, -100, 10, 10);  // or similar
n.Show();
setFormLocation(n, screens[1]);

回答by Gengi

this.Location = Screen.AllScreens[1].WorkingArea.Location;

this is the Form reference.

这是表格参考。

回答by Eric

I used this for an XNA 4 Dual Screen Application (Full Screen XNA Game Window + WinForm)

我将它用于 XNA 4 双屏应用程序(全屏 XNA 游戏窗口 + WinForm)

In the Form_Load() method, place the following code:

在 Form_Load() 方法中,放置以下代码:

var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);  
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;

this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);

回答by ravikumar

Set form Startup Position property to Manual

将表单启动位置属性设置为手动

    public void MoveWindowToProjector ()  
    { 
           Rectangle rectMonitor;

            // Create New Process
            Process objProcess = new Process();

            //Get All the screens associated with this Monitor
            Screen[] screens = Screen.AllScreens;

            // Get Monitor Count
            int iMonitorCount = Screen.AllScreens.Length;

            // Get Parameters of Current Project
            string[] parametros = Environment.GetCommandLineArgs();
           // if (parametros.Length > 0)
           // {
                //objProcess.StartInfo.FileName = parametros[0];
               // objProcess.Start();
          //  }
            // Get Window Handle of this Form
            IntPtr hWnd = this.Handle;

            Thread.Sleep(1000);


            if (IsProjectorMode)
            {
                if (iMonitorCount > 1) // If monitor Count 2 or more
                {
                    //Get the Dimension of the monitor
                    rectMonitor = Screen.AllScreens[1].WorkingArea;
                }
                else
                {
                    //Get the Dimension of the monitor
                    rectMonitor = Screen.AllScreens[0].WorkingArea;
                }

            }
            else
            {
                rectMonitor = Screen.AllScreens[0].WorkingArea;

            }
            if (hWnd != IntPtr.Zero)
            {
                SetWindowPos(hWnd, 0,
                    rectMonitor.Left, rectMonitor.Top, rectMonitor.Width,
                    rectMonitor.Height, SWP_SHOWWINDOW);
            }

        }

回答by Adham Sabry

To display form on secondary screen:

要在辅助屏幕上显示表单:

    Screen primaryFormScreen = Screen.FromControl(primaryForm);
    //Use this if you are looking for secondary screen that is not primary
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
    //Use this if you are looking for screen that is not being used by specific form
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
    //Putting the form on the other screen
    secondaryForm.Left = secondaryFormScreen.Bounds.Width;
    secondaryForm.Top = secondaryFormScreen.Bounds.Height;
    //Recommended to use, You can change it back later to the settings you wish
    secondaryForm.StartPosition = FormStartPosition.Manual;
    secondaryForm.Location = secondaryFormScreen.Bounds.Location;
    Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
    secondaryForm.Location = p;
    secondaryForm.Show();

If you are looking forward for a specific screen, you can loop on "Screen.AllScreens" and use the process above.

如果您期待特定屏幕,您可以在“Screen.AllScreens”上循环并使用上述过程。

回答by Alex Strickland

@Gengi's answer is succinct and works well. If the window is maximised it does not move the window. This snippet solves that (although I suspect the windows "normal" dimensions must be smaller than the new screen dimensions for this to work):

@Gengi 的回答简洁明了,效果很好。如果窗口最大化,它不会移动窗口。这段代码解决了这个问题(尽管我怀疑窗口的“正常”尺寸必须小于新的屏幕尺寸才能正常工作):

    void showOnScreen(int screenNumber)
    {
        Screen[] screens = Screen.AllScreens;

        if (screenNumber >= 0 && screenNumber < screens.Length)
        {
            bool maximised = false;
            if (WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;
                maximised = true;
            }
            Location = screens[screenNumber].WorkingArea.Location;
            if (maximised)
            {
                WindowState = FormWindowState.Maximized;
            }
        }
    }

回答by Moory Pc

  Screen[] screens = Screen.AllScreens;
                sc aoc = new sc();
                aoc.Show();
    aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;

FOR MAXIMIZED WINDOW STATE

最大化窗口状态

aoc.WindowState = FormWindowState.Maximized;

FOR ANY X,Y POSITION

对于任何 X,Y 位置

aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);

回答by Air196

This method shows forms on selected screen from left to right:

此方法在所选屏幕上从左到右显示表单:

void ShowFormsOnScreenLeftToRight(Screen screen, params Form[] forms)
    {
        if (forms == null || forms.Length == 0)
            return;

        var formsCnt = forms.Length;
        var formSize = new Size(screen.WorkingArea.Size.Width / formsCnt, screen.WorkingArea.Size.Height);
        for (var i = 0; i < formsCnt; i++)
        {
            var form = forms[i];
            if (form == null)
                continue;
            form.WindowState = FormWindowState.Normal;
            form.Location = new Point(screen.WorkingArea.Left + screen.WorkingArea.Size.Width / formsCnt * i, 0);
            form.Size = formSize;
            form.BringToFront();
        }
    }

To solve your problem, you should run:

要解决您的问题,您应该运行:

ShowFormsOnScreenLeftToRight(n, Screen.AllScreens.First(s => !s.Primary));