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

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

}

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


慕容3067478
浏览 352回答 4
4回答

翻过高山走不出你

尝试在SetFormLocation方法中将WindowStartUpLocation参数设置为“ manual”。

呼啦一阵风

this.Location = Screen.AllScreens[1].WorkingArea.Location;这是表单参考。

有只小跳蛙

效果很好。如果窗口最大化,则不会移动窗口。此代码段解决了这一问题(尽管我怀疑窗口的“正常”尺寸必须小于新的屏幕尺寸才能正常工作):&nbsp; &nbsp; void showOnScreen(int screenNumber)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Screen[] screens = Screen.AllScreens;&nbsp; &nbsp; &nbsp; &nbsp; if (screenNumber >= 0 && screenNumber < screens.Length)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool maximised = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (WindowState == FormWindowState.Maximized)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WindowState = FormWindowState.Normal;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maximised = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Location = screens[screenNumber].WorkingArea.Location;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (maximised)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WindowState = FormWindowState.Maximized;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP