【问题标题】:How to open form2 on the second monitor?如何在第二台显示器上打开form2?
【发布时间】:2017-08-01 15:52:21
【问题描述】:

我从 Form1 创建 Form2。我希望 Form2 在第二台显示器上打开。我怎么能做到这一点? 我使用此代码:

private void button1_Click(object sender, EventArgs e)
{
    Form2 dlg = new Form2();
    dlg.Show();
}

如何为此更改此代码? 谢谢大家。

【问题讨论】:

标签: c#


【解决方案1】:

使用此代码

Form2 dlg = new Form2();
Screen[] screens = Screen.AllScreens;
Rectangle bounds = screen[1].Bounds;
dlg.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
dlg.StartPosition = FormStartPosition.Manual;
dlg.Show();

【讨论】:

  • 我不确定只有这样才能解决她的问题。她需要以某种方式设置表单的Position
  • 如何设置表格的位置?
【解决方案2】:

试试这样的

Screen[] sc; 
sc = Screen.AllScreens; 
//get all the screen width and heights 
Form2 f = new Form2(); 
f.FormBorderStyle = FormBorderStyle.None; 
f.Left = sc[neededmonitor].Bounds.Width; 
f.Top = sc[neededmonitor].Bounds.Height; 
f.StartPosition = FormStartPosition.Manual; 
f.Show(); 

【讨论】:

  • 但是我可以检查一下 if (exist.sc[1]) { 在第二台显示器上显示表单} else { 在 1 台显示器上}。 sc[1] = 什么类型的数据?
  • 数据类型为屏幕。您可以检查 sc 的长度。长度 = 允许的屏幕数。 if(sc.Length>1) { 在第二台显示器上显示表格}
【解决方案3】:

您可以像这样检测辅助监视器(使用 System.Linq):

var screen = Screen.AllScreens.FirstOrDefault(s => !s.Primary && s.DeviceName.Contains("DISPLAY2"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多