C# 第二个监视器上的全屏窗口

C# 第二个监视器上的全屏窗口,c#,.net,windows,winforms,C#,.net,Windows,Winforms,我想写一个程序,在两个屏幕上打开一个窗口。 我希望两台显示器上的两个窗口都是全屏的 所以我使用了win.forms功能屏幕。所有屏幕都显示为 那样做。在第一个屏幕上,一切正常。但是在 辅助屏幕窗口不是全屏 所以我写了一些代码来显示两个监视器的值。 我看到监视器2的值不正确。两台显示器均为192 x 1080 但第二个监视器显示为1536 x 864 if (System.Windows.Forms.SystemInformation.MonitorCount < 2)

我想写一个程序,在两个屏幕上打开一个窗口。 我希望两台显示器上的两个窗口都是全屏的

所以我使用了win.forms功能屏幕。所有屏幕都显示为 那样做。在第一个屏幕上,一切正常。但是在 辅助屏幕窗口不是全屏

所以我写了一些代码来显示两个监视器的值。 我看到监视器2的值不正确。两台显示器均为192 x 1080

但第二个监视器显示为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();
         }
     }
if(System.Windows.Forms.SystemInformation.MonitorCount<2)
{
primaryScreen=System.Windows.Forms.Screen.primaryScreen;
}
其他的
{
如果(屏幕编号==2)
{
primaryScreen=System.Windows.Forms.Screen.AllScreens[1];
secondaryScreen=System.Windows.Forms.Screen.PrimaryScreen;
}
其他的
{
primaryScreen=System.Windows.Forms.Screen.primaryScreen;
secondaryScreen=System.Windows.Forms.Screen.AllScreens[0];
}
}
如果(屏幕编号==2)
{
ScreenTwo newWindow=新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();
}
其他的
{
ScreenOne newWindow=新ScreenOne();
newWindow.WindowsStartUplocation=WindowsStartUplocation.CenterScreen;
newWindowWindowStyle=WindowsStyle.SingleBorderWindow;
newWindow.ShowInTaskbar=true;
newWindow.ResizeMode=ResizeMode.CanResizeWithGrip;
newWindow.WindowsState=WindowsState.Maximized;
newWindow.Show();
}
}
我确实在窗口2上将WindowsState设置为最大化,但窗口2在屏幕1上打开。 有人知道我如何解决这个问题吗?


你打开窗口的代码没有引用你想要的屏幕,所以是的,它会出现在主屏幕上看起来像WPF而不是WinForms?!?(或者两者兼而有之)是的,但图书馆是一样的,所以我认为适应它应该不会太难。如果不是这样的话,我会删除我的答案。看来WindowsStartUplocation as手册是重要的一部分,这里有一个表单解决方案,谢谢这项工作:)
    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;
            }
        }
    }
}