Going fullscreen on secondary monitor Going fullscreen on secondary monitor wpf wpf

Going fullscreen on secondary monitor


Extension method to Maximize a window to the secondary monitor (if there is one).Doesn't assume that the secondary monitor is System.Windows.Forms.Screen.AllScreens[2];

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


For WPF apps look at this post. Ultimately it depends on when the WindowState is set to Maximized. If you set it in XAML or in window constructor (i.e. before the window is loaded) it will always be maximized onto primary display. If, on the other hand, you set WindowState to Maximized when the window is loaded - it will maximise on the screen on which it was maximized before.


I notice an answer which advocates setting the position in the Loaded event, but this causes flicker when the window is first shown normal then maximized. If you subscribe to the SourceInitialized event in your constructor and set the position in there it will handle maximizing onto secondary monitors without flicker - I'm assuming WPF here

public MyWindow(){    SourceInitialized += MyWindow_SourceInitialized;}void MyWindow_SourceInitialized(object sender, EventArgs e){    Left = 100;    Top = 50;    Width = 800;    Height = 600;    WindowState = WindowState.Maximized;}

Substitute coords for any on your secondary monitor