1

I'm trying to write a program that opens a window on two screens. I want both windows on both monitors in full-screen.

So i used the win.forms function screen. All screens to do that. On the first screen everything is fine. but on the secondary screen the windows is not in full-screen.

So i wrote some code to show me the values of both monitors. I saw that the values of monitor 2 is not correct. Both monitors are on 192 x 1080.

but the second monitor is shown as 1536 x 864.

       if (System.Windows.Forms.SystemInformation.MonitorCount < 2)
         {
             primaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
         }
         else
         {
             if (screennumber == 2)
             {
                 primaryScreen = System.Windows.Forms.Screen.AllScreens[1];
                 secondaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
             }
             else
             {
                 primaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
                 secondaryScreen = System.Windows.Forms.Screen.AllScreens[0];
             }
         }

        if (screennumber == 2)
         {
             ScreenTwo newWindow = new ScreenTwo();
             newWindow.WindowStartupLocation = WindowStartupLocation.Manual;
             newWindow.Left = m_PrimaryScreen.WorkingArea.Left;
             newWindow.Top = m_PrimaryScreen.WorkingArea.Top;
             newWindow.Width = m_PrimaryScreen.WorkingArea.Width;
             newWindow.Height = m_PrimaryScreen.WorkingArea.Height;
             newWindow.Show();
         }
         else
         {
             ScreenOne newWindow = new ScreenOne();
             newWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
             newWindowWindowStyle = WindowStyle.SingleBorderWindow;
             newWindow.ShowInTaskbar = true;
             newWindow.ResizeMode = ResizeMode.CanResizeWithGrip;
             newWindow.WindowState = WindowState.Maximized;
             newWindow.Show();
         }
     }

I did set the windowstate to maximize on window 2 but then window 2 opens on screen one. Anybody know how I can fix this???

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
OHA
  • 303
  • 4
  • 18
  • 1
    Your code for opening windows doesnt make any reference to the screen you want it on, so yes, it will appear on the primary screen – BugFinder Aug 15 '16 at 12:44

1 Answers1

1

Going fullscreen on secondary monitor

    using System.Linq;
    using System.Windows;

    namespace ExtendedControls

{
    static public class WindowExt
    {

        // NB : Best to call this function from the windows Loaded event or after showing the window
        // (otherwise window is just positioned to fill the secondary monitor rather than being maximised).
        public static void MaximizeToSecondaryMonitor(this Window window)
        {
            var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();

            if (secondaryScreen != null)
            {
                if (!window.IsLoaded)
                    window.WindowStartupLocation = WindowStartupLocation.Manual;

                var workingArea = secondaryScreen.WorkingArea;
                window.Left = workingArea.Left;
                window.Top = workingArea.Top;
                window.Width = workingArea.Width;
                window.Height = workingArea.Height;
                // If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
                if ( window.IsLoaded )
                    window.WindowState = WindowState.Maximized;
            }
        }
    }
}
Community
  • 1
  • 1
hYg-Cain
  • 348
  • 3
  • 10
  • 1
    Looks like WPF rather than WinForms?!? (Or a mix of both). – Uwe Keim Aug 15 '16 at 12:45
  • It is but the libraries are the same, so I thought it shouldn't be too hard to adapt it. If this is not the case I will delete my answer. – hYg-Cain Aug 15 '16 at 12:49
  • It seems that the WindowStartUpLocation as manual is the important part Here is a solution for forms http://stackoverflow.com/questions/1363374/showing-a-windows-form-on-a-secondary-monitor – hYg-Cain Aug 15 '16 at 12:56
  • thanks this works :) – OHA Aug 15 '16 at 13:02