2017-06-21 42 views
3

有沒有什麼辦法讓WPF應用程序在每個系統規模上都能獲得相同的大小?WPF應用程序每個系統規模相同的大小(規模無關)

當我改變文本,應用程序和從125%的Windows系統設置等物品改變大小(推薦),在全高清屏幕100%,我的WPF應用程序變得太小。爲了實現獨立的系統規模化應用,我寫了這樣的功能來改變我的應用程序的縮放回到125%:

private void ScaleTo125Percents() 
{ 
    // Change scale of window content 
    MainContainer.LayoutTransform = new ScaleTransform(1.25, 1.25, 0, 0); 
    Width *= 1.25; 
    Height *= 1.25; 

    // Bring window center screen 
    var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight; 
    var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth; 
    Top = (screenHeight - Height)/2; 
    Left = (screenWidth - Width)/2; 
} 

但還有條件來調用這個函數。第一個屏幕必須是Full-HD(有API來檢查這個),並且系統規模必須是100%(沒有.NET API來獲得系統規模)。

我該怎麼辦?我是否正在做標準方法來使我的應用系統的規模獨立?我見過的規模獨立的應用程序

例子:

  • 的Visual Studio 2017年安裝
  • 電報桌面
+0

檢查出來的'Viewbox'佈局容器:https://blog.udemy.com/wpf-viewbox/ –

+0

@BradleyUffner'Viewbox'變化元素通過調整大小來縮放。我想要一個基於系統設置的靜態縮放。我認爲這沒有幫助。 – 0xaryan

+0

當你說「獨立衡量」時,我不認爲我完全理解你所要求的。 'Viewbox'可以給你一個特定虛擬大小的畫布。放置在其中的所有內容都將顯示並佈置,就好像它的「Viewbox」始終是指定的大小,而不管應用程序的窗口大小,還是系統DPI。也許一幅關於你的內容如何以不同大小表現的圖表會有所幫助。 –

回答

2

終於找到了答案。首先獲得使用下列選項之一DPI系統規模:

  • 閱讀從位於Computer\HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics註冊表AppliedDPI DWORD。然後,通過96
  • 劃分,或使用這個片段:

    double dpiFactor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11; 
    

    返回至2.5

1.0之間的值,然後創建一個保存應用程序設置的配置文件,並設置dpiFactor爲默認比例。如果用戶想要的定製規模,呼籲窗口啓動此功能:

private void UserInterfaceCustomScale(double customScale) 
{ 
    // Change scale of window content 
    MainContainer.LayoutTransform = new ScaleTransform(customScale, customScale, 0, 0); 
    Width *= customScale; 
    Height *= customScale; 

    // Bring window center screen 
    var screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight; 
    var screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth; 
    Top = (screenHeight - Height)/2; 
    Left = (screenWidth - Width)/2; 
}