2013-11-26 99 views
0

我一直在WPF桌面應用程序的工作,並根據特定的屏幕分辨率的圖形。我需要根據屏幕分辨率縮放所有邊距。我怎樣才能做到這一點?WPF屏幕分辨率

+1

檢查這個http://stackoverflow.com/questions/1927540/how-to-get-the-size-of-the-current-screen-in-wpf 或這個 – Akrem

+0

多屏幕問題的鏈接的答案,我不在乎多屏幕截至目前,什麼是「這個」在你的評論中指的是,它不是一個鏈接 – androider

回答

1

你可以使用:

System.Windows.SystemParameters.PrimaryScreenWidth; 
System.Windows.SystemParameters.PrimaryScreenHeight; 

稍作搜索:)

+1

是的,我做到了,但我會如何調整margin.For例如:設計爲1366x768。但是這個保證金將被設置爲獨立於分辨率的所有屏幕。我需要回答這個問題,如果它是一個有效的問題,並且它有意義 – androider

+0

@yshooooo在這種情況下,您最好查找Grid.RowDefinitions和ColumnDefinitions – Akrem

+0

所以你的意思是說,如果textblock的寬度爲30,那麼我應該創建一個並將其ColumnDefinitions定義爲1 *和2 *。我想這將是很多工作,而且我也無法達到所有定義的準確性。有沒有其他的方式? – androider

0

爲了讓我們必須使用系統的DPI因子值,可以使用下面的代碼爲每this articleScreen Resolutions(請閱讀它完全理解):

using System.Windows.Media; 


Window mainWindow = Application.Current.MainWindow; 
PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(mainWindow); 
Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice; 
double dpiWidthFactor = m.M11; 
double dpiHeightFactor = m.M22; 
double ScreenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor; //calculated correct value 
double ScreenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor; //calculated correct value 
+0

mainWindowPresentationSource對我來說始終爲空 - 爲什麼? –

+0

@MarkRichman在調用這段代碼之前,應該先加載你的MainWindow對象,我想這就是你得到null的原因。嘗試在Load完成時將代碼放入Window.Loded事件或其他事件中。 – VS1