2016-08-15 95 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 –

相關問題