我正在编写一个简单的 Windows 窗体应用程序。我希望表单仅在主屏幕中打开,无论在哪个屏幕中调用应用程序(假设用户有多个屏幕)。主屏幕的目的是用于显示 Windows 任务栏的屏幕。
谢谢,毛尔。
我正在编写一个简单的 Windows 窗体应用程序。我希望表单仅在主屏幕中打开,无论在哪个屏幕中调用应用程序(假设用户有多个屏幕)。主屏幕的目的是用于显示 Windows 任务栏的屏幕。
谢谢,毛尔。
你可以试试这个:
/// <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);
}
您应该将主窗体的 IsMdiParent 属性设置为 yes。
当你展示其他表格时,你应该像这样展示它们:
private void MainForm_Load(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
}
这将在您的主窗体中打开 Form2。