Going fullscreen on secondary monitor

前端 未结 7 1636
离开以前
离开以前 2020-11-30 02:31

How can you program a dotNet Windows (or WPF) Application in order to let it going fullscreen on the secondary monitor?

7条回答
  •  眼角桃花
    2020-11-30 03:06

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

提交回复
热议问题