51

我正在尝试在辅助监视器上设置 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;
}

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

4

9 回答 9

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

这是表单参考。

于 2012-07-30T12:07:00.557 回答
39

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

于 2009-09-01T16:28:42.027 回答
12

@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;
            }
        }
    }
于 2015-12-14T09:16:07.630 回答
3

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


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

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

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

n.SetBounds(-100, -100, 10, 10);  // or similar
n.Show();
setFormLocation(n, screens[1]);
于 2009-09-01T16:30:11.553 回答
2

在辅助屏幕上显示表单:

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

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

于 2015-08-18T17:00:51.083 回答
1

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

在 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);
于 2013-10-19T14:56:31.553 回答
0

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

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

        }
于 2015-02-02T09:41:23.347 回答
0

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

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

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

ShowFormsOnScreenLeftToRight(n, Screen.AllScreens.First(s => !s.Primary));
于 2020-03-05T11:01:11.783 回答
-2
  Screen[] screens = Screen.AllScreens;
                sc aoc = new sc();
                aoc.Show();
    aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;

最大化窗口状态

aoc.WindowState = FormWindowState.Maximized;

适用于任何 X、Y 位置

aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);
于 2016-09-29T10:25:58.820 回答