2016-08-15 87 views
1

我正试图编写一个程序,在两个屏幕上打开一个窗口。 我希望全屏显示两个显示器上的两个窗口。第二台显示器上的全屏窗口

所以我使用了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(); 
     } 
    } 

我并设置将WindowState最大化窗口2,但然后窗口2在屏幕上一个打开。 任何人都知道我可以如何解决这个问题?

+1

您的打开窗户犯规让你想要它在屏幕上的任何引用代码,所以是的,它会出现在主屏幕上 – BugFinder

回答

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

看起来像WPF,而不是WinForms的?!? (或两者的混合)。 –

+0

这只是图书馆是一样的,所以我认为它不应该太难以适应它。如果情况并非如此,我会删除我的答案。 –

+0

看来,作为手册的WindowStartUpLocation是重要的部分 这是一个解决方案形式 http://stackoverflow.com/questions/1363374/showing-a-windows-form-on-a-secondary-monitor –

相关问题