【问题标题】:How to make the windows form open only on the main screen [duplicate]如何使窗口窗体仅在主屏幕上打开[重复]
【发布时间】:2018-07-20 11:33:53
【问题描述】:

我正在编写一个简单的 Windows 窗体应用程序。 我希望表单仅在主屏幕中打开,无论在哪个屏幕中调用应用程序(假设用户有多个屏幕)。 主屏幕的目的是用于显示 windows 任务栏的屏幕。

谢谢,毛尔。

【问题讨论】:

  • Get primary screen 很简单,那么只需set form location
  • 嗨,@rs232 那里提出的问题与我提出的不同。我不希望窗口会在它调用的屏幕上打开,我希望窗口总是会在操作系统的主屏幕上打开。 (用那个任务栏)
  • 谢谢@Sinatr,我想这就是我想要的。
  • @rs232 大声笑,那个问题中的 OP 已经知道如何做这个问题中的 OP 想要的。虽然他在问题中给出的例子可能会回答这个问题。
  • 你可以和设计师一起做。确保 Location 属性仍然是 (0,0),然后只需将 StartPosition 设置为 Manual。

标签: c# .net winforms mainscreen


【解决方案1】:

你可以试试这个:

/// <summary>
/// Sets the location of the form to primary screen.
/// </summary>
/// <param name="this">Instance of the form.</param>
/// <param name="x">The x coordinate of the form's location.</param>
/// <param name="y">The y coordinate of the form's location.</param>
public static void SetPrimaryLocation(this Form @this, int x = 0, int y = 0)
{
    var rect = Screen.PrimaryScreen.WorkingArea;
    @this.Location = new Point(rect.X + x, rect.Y + y);
}

【讨论】:

    【解决方案2】:

    您应该将主窗体的 IsMdiParent 属性设置为 yes。

    当你展示其他表格时,你应该像这样展示它们:

        private void MainForm_Load(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.MdiParent = this;
            frm.Show();
        }
    

    这将在您的主窗体中打开 Form2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多