如何让windows窗体只在主屏幕上打开

Mao*_*r.I 2 .net c# winforms mainscreen

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

谢谢,马奥尔。

小智 5

你可以试试这个:

/// <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);
}
Run Code Online (Sandbox Code Playgroud)