外出全屏辅助监视器上全屏、器上

2023-09-02 01:40:16 作者:我只用七天七夜思念你

你怎么能方案,以一个dotNet的Windows(或WPF)应用程序,让它外出时的全屏辅助监视器上?

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

推荐答案

扩展方法,以最大化的窗口来辅助监视器(如果有的话)。 不假定辅助显示器是System.Windows.Forms.Screen.AllScreens [2];

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;
            }
        }
    }
}
 
精彩推荐
图片推荐