2016-07-12 60 views
0

我正在將應用程序調整爲自定義DPI值(Windows縮放)。自定義DPI輔助屏幕的實際分辨率

如果我理解正確,縮放使應用程序認爲,分辨率比實際情況要小。例如,具有125%的縮放比例,如果您的分辨率爲1920x1080,則最大化窗口的實際大小將爲1536x864。當然,如果你通過代碼控制一些尺寸和位置,它可能會導致一些問題。

現在,我使用的下一個值用於定位等:

  • SystemParameters.FullPrimaryScreenWidthSystemParameters.FullPrimaryScreenHeight 「虛擬」(縮放)分辨率;
  • Screen.PrimaryScreen.WorkingArea.Width and Screen.PrimaryScreen.WorkingArea.Height原始分辨率;

從這個值,很容易得到縮放值,它可以用於任何定位和大小。

但所有這些值實際上是主屏幕。如果我需要爲輔助屏幕進行定位和調整大小,這可能具有不同的實際(因此不同的「虛擬」)分辨率?

現在我越來越屏這樣

var screen = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow).Handle); 

,讓我得到實際的分辨率,使用WorkingArea矩形。但是,「虛擬」解決方案呢?除主要任何屏幕外,是否有類似SystemParameters.FullPrimaryScreenWidth等?

PS。我知道,這個比例是共享的,所以通過獲得主屏幕的價值,我可以將它用於任何其他屏幕,但我想知道答案,無論如何,如果有直接變量來獲取此值。

+0

WPF應用程序絕不會發生這種情況,它默認會禁用DPI虛擬化。當然,你所犯的錯誤是你忘了將你從屏幕類中得到的值從像素轉換成英寸。 [看這裏](http://stackoverflow.com/questions/30424985/how-to-convert-a-wpf-inch-unit-to-winforms-pixels-and-vice-versa)。 –

+0

@HansPassant無論如何,主要的問題是如何獲得「虛擬」的解決方案。 – lentinant

回答

0

你可以用下面的比

 // calculates text size in that main window (i.e. 100%, 125%,...) 
     var ratio = Math.Max(Screen.PrimaryScreen.WorkingArea.Width/SystemParameters.PrimaryScreenWidth, 
         Screen.PrimaryScreen.WorkingArea.Height/SystemParameters.PrimaryScreenHeight); 

例如,如果你想顯示所有的屏幕上最大化的窗口,你需要使用下面的矩形(代碼是從App.xaml.cs) :

 foreach (var screen in Screen.AllScreens) 
     { 
      var mainViewModel = container.Resolve<IMainWindowModel>(); 

      var window = new MainWindow(mainViewModel); 
      if (screen.Primary) 
       Current.MainWindow = window; 

      window.Left = screen.WorkingArea.Left/ratio; 
      window.Top = screen.WorkingArea.Top/ratio; 
      window.Width = screen.WorkingArea.Width/ratio; 
      window.Height = screen.WorkingArea.Height/ratio; 
      window.Show(); 
      window.WindowState = WindowState.Maximized; 
     } 

另外,你可以看看我的文章https://ireznykov.com/2017/01/13/wpf-windows-on-two-screens/,這指的是產出最大化所有畫面上的窗口GitHub的示例應用程序。