Search code examples
c#.netwinformsmainscreen

How to make the windows form open only on the main screen


I am writing a simple windows form application. I want that the form will open only in the main screen, no matter in which screen the application was invoked (Assuming that the user has more than one screen). The purpose of the main screen is for the screen that shows the windows taskbar.

Thanks, Maor.


Solution

  • You can try this:

    /// <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);
    }