Screen Resolution Problem In WPF?

后端 未结 8 1226
孤城傲影
孤城傲影 2020-11-30 00:13

I\'m gonna detect the resolution with the following code in WPF :

double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = S         


        
8条回答
  •  一整个雨季
    2020-11-30 00:35

    For an even more robust implementation, you should calculate the DPI factors on your system and work with those factors. A normal DPI value is 96, but some monitors may have different values. Consider that your code may be running on a monitor that has a different DPI value than 96. Consider this code:

        private static void CalculateDpiFactors()
        {
            Window MainWindow = Application.Current.MainWindow;
            PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
            Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
            thisDpiWidthFactor = m.M11;
            thisDpiHeightFactor = m.M22;
        }
    

    You can then use those ratios to get the final values:

    CalculateDpiFactors();
    double ScreenHeight = SystemParameters.PrimaryScreenHeight * thisDpiHeightFactor;
    double ScreenWidth = SystemParameters.PrimaryScreenWidth * thisDpiWidthFactor;
    

    The values of ScreenHeight and ScreenWidth should then match what you see in your monitor's Properties window.

提交回复
热议问题