Screen Resolution Problem In WPF?

后端 未结 8 1241
孤城傲影
孤城傲影 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:47

    Please call this after your windows is loaded.

     public static class Ext
    {
        public static Size GetNativePrimaryScreenSize(this Window window)
        {
            PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(window);
            Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
            var dpiWidthFactor = m.M11;
            var dpiHeightFactor = m.M22;
            double screenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor;
            double screenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor;
    
            return new Size(screenWidth, screenHeight);
        }
    }
    

提交回复
热议问题