Showing a Windows form on a secondary monitor?
Asked Answered
C

9

55

I'm trying to set a Windows Form on secondary monitor, as follows:

private void button1_Click(object sender, EventArgs e)
{
    MatrixView n = new MatrixView();
    Screen[] screens = Screen.AllScreens;
    setFormLocation(n, screens[1]);
    n.Show();
}

private void setFormLocation(Form form, Screen screen)
{
    // first method
    Rectangle bounds = screen.Bounds;
    form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);

    // second method
    //Point location = screen.Bounds.Location;
    //Size size = screen.Bounds.Size;

    //form.Left = location.X;
    //form.Top = location.Y;
    //form.Width = size.Width;
    //form.Height = size.Height;
}

The properties of bounds seem correct, but in both methods I've tried, this maximizes the form on the primary monitor. Any ideas?

Cotto answered 1/9, 2009 at 16:19 Comment(2)
Just to be sure, the WindowState on MatrixView isn't Maximized, is it?Cangue
@Austin No, the WindowState is Normal.Cotto
M
44

Try setting StartPosition parameter as FormStartPosition.Manual inside your SetFormLocation method.

Margetmargette answered 1/9, 2009 at 16:28 Comment(6)
Yeah, doing form.StartPosition = FormStartPosition.Manual; did the trick. Any idea why?Cotto
@Henk No, it's Windows Forms. Here's a link to it: msdn.microsoft.com/en-us/library/…Cotto
From MSDN: "Setting WindowStartupLocation to Manual causes a window to be positioned according to its Left and Top property values. If either the Left or Top properties aren't specified, their values are determined by Windows." msdn.microsoft.com/en-us/library/…Margetmargette
where is WindowStartUpLocation? I can't find itShedevil
If your form is maximized you have firstly to set this.WindowState = FormWindowState.Normal; and then move form to other screenSanitary
is it possible to force resolution to 4: 3?Billiot
M
63
this.Location = Screen.AllScreens[1].WorkingArea.Location;

this is the Form reference.

Mullin answered 30/7, 2012 at 12:7 Comment(2)
However it seems that it is ordered by some other criteria than Window's monitor configuration. My primary for example was at position [1] while my second monitor was at [0].Companionate
will not work if you set location where from you creating form instance. like works on form loadKooima
M
44

Try setting StartPosition parameter as FormStartPosition.Manual inside your SetFormLocation method.

Margetmargette answered 1/9, 2009 at 16:28 Comment(6)
Yeah, doing form.StartPosition = FormStartPosition.Manual; did the trick. Any idea why?Cotto
@Henk No, it's Windows Forms. Here's a link to it: msdn.microsoft.com/en-us/library/…Cotto
From MSDN: "Setting WindowStartupLocation to Manual causes a window to be positioned according to its Left and Top property values. If either the Left or Top properties aren't specified, their values are determined by Windows." msdn.microsoft.com/en-us/library/…Margetmargette
where is WindowStartUpLocation? I can't find itShedevil
If your form is maximized you have firstly to set this.WindowState = FormWindowState.Normal; and then move form to other screenSanitary
is it possible to force resolution to 4: 3?Billiot
O
12

@Gengi's answer is succinct and works well. If the window is maximised it does not move the window. This snippet solves that (although I suspect the windows "normal" dimensions must be smaller than the new screen dimensions for this to work):

    void showOnScreen(int screenNumber)
    {
        Screen[] screens = Screen.AllScreens;

        if (screenNumber >= 0 && screenNumber < screens.Length)
        {
            bool maximised = false;
            if (WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;
                maximised = true;
            }
            Location = screens[screenNumber].WorkingArea.Location;
            if (maximised)
            {
                WindowState = FormWindowState.Maximized;
            }
        }
    }
Odontology answered 14/12, 2015 at 9:16 Comment(0)
C
3

Are you sure screens[1] is your secondary? Give screens[0] a try. Your code is basically correct.


Ok, I checked, you will have to do it after the Show():

n.Show();
setFormLocation(n, screens[1]);

which gives some unwanted flicker. But you can probably do:

n.SetBounds(-100, -100, 10, 10);  // or similar
n.Show();
setFormLocation(n, screens[1]);
Cagey answered 1/9, 2009 at 16:30 Comment(1)
Relatively sure - using screens[0] and screens[1] give the same result.Cotto
S
3

To display form on secondary screen:

    Screen primaryFormScreen = Screen.FromControl(primaryForm);
    //Use this if you are looking for secondary screen that is not primary
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
    //Use this if you are looking for screen that is not being used by specific form
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
    //Putting the form on the other screen
    secondaryForm.Left = secondaryFormScreen.Bounds.Width;
    secondaryForm.Top = secondaryFormScreen.Bounds.Height;
    //Recommended to use, You can change it back later to the settings you wish
    secondaryForm.StartPosition = FormStartPosition.Manual;
    secondaryForm.Location = secondaryFormScreen.Bounds.Location;
    Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
    secondaryForm.Location = p;
    secondaryForm.Show();

If you are looking forward for a specific screen, you can loop on "Screen.AllScreens" and use the process above.

Saransk answered 18/8, 2015 at 17:0 Comment(2)
What is secondaryForm and where is it declared in your or OP's code?Isochroous
secondaryForm is a Form type of object where i set it's location to be within the secondaryFormScreen bounds. It doesn't have to be Form depending on what you are using, you can set similar bounds the same way i did to the form to allocate within the other screenSaransk
D
1

I used this for an XNA 4 Dual Screen Application (Full Screen XNA Game Window + WinForm)

In the Form_Load() method, place the following code:

var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);  
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;

this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);
Diaconal answered 19/10, 2013 at 14:56 Comment(1)
I know I'm late on this, but for anyone else looking at this. Primary display is NOT always element 0 in the list, so this doesn't reliably get the primary display. However this did lead me to the solution using the Primary property of Screen. var primaryMonitor = Screen.AllScreens.Where(s => s.Primary).FirstOrDefault(); var secondaryScreens = Screen.AllScreens.Where(s => !s.Primary).ToList();Tver
O
0

Set form Startup Position property to Manual

    public void MoveWindowToProjector ()  
    { 
           Rectangle rectMonitor;

            // Create New Process
            Process objProcess = new Process();

            //Get All the screens associated with this Monitor
            Screen[] screens = Screen.AllScreens;

            // Get Monitor Count
            int iMonitorCount = Screen.AllScreens.Length;

            // Get Parameters of Current Project
            string[] parametros = Environment.GetCommandLineArgs();
           // if (parametros.Length > 0)
           // {
                //objProcess.StartInfo.FileName = parametros[0];
               // objProcess.Start();
          //  }
            // Get Window Handle of this Form
            IntPtr hWnd = this.Handle;

            Thread.Sleep(1000);


            if (IsProjectorMode)
            {
                if (iMonitorCount > 1) // If monitor Count 2 or more
                {
                    //Get the Dimension of the monitor
                    rectMonitor = Screen.AllScreens[1].WorkingArea;
                }
                else
                {
                    //Get the Dimension of the monitor
                    rectMonitor = Screen.AllScreens[0].WorkingArea;
                }

            }
            else
            {
                rectMonitor = Screen.AllScreens[0].WorkingArea;

            }
            if (hWnd != IntPtr.Zero)
            {
                SetWindowPos(hWnd, 0,
                    rectMonitor.Left, rectMonitor.Top, rectMonitor.Width,
                    rectMonitor.Height, SWP_SHOWWINDOW);
            }

        }
Onepiece answered 2/2, 2015 at 9:41 Comment(0)
M
0

This method shows forms on selected screen from left to right:

void ShowFormsOnScreenLeftToRight(Screen screen, params Form[] forms)
    {
        if (forms == null || forms.Length == 0)
            return;

        var formsCnt = forms.Length;
        var formSize = new Size(screen.WorkingArea.Size.Width / formsCnt, screen.WorkingArea.Size.Height);
        for (var i = 0; i < formsCnt; i++)
        {
            var form = forms[i];
            if (form == null)
                continue;
            form.WindowState = FormWindowState.Normal;
            form.Location = new Point(screen.WorkingArea.Left + screen.WorkingArea.Size.Width / formsCnt * i, 0);
            form.Size = formSize;
            form.BringToFront();
        }
    }

To solve your problem, you should run:

ShowFormsOnScreenLeftToRight(n, Screen.AllScreens.First(s => !s.Primary));
Marks answered 5/3, 2020 at 11:1 Comment(0)
G
-2
  Screen[] screens = Screen.AllScreens;
                sc aoc = new sc();
                aoc.Show();
    aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;

FOR MAXIMIZED WINDOW STATE

aoc.WindowState = FormWindowState.Maximized;

FOR ANY X,Y POSITION

aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);
Gypsy answered 29/9, 2016 at 10:25 Comment(1)
Doesnt work for me, it just puts it on my primary monitor nomatter whatLoveinamist

© 2022 - 2024 — McMap. All rights reserved.