是否有更好或更优雅的方法来找出实际显示器的有效像素尺寸?

时间:2017-03-31 17:18:03

标签: c# uwp windows-10-universal

我目前正在开发几个UWP应用程序,我想利用应用程序运行的显示器/屏幕的有效像素尺寸信息。有没有比我做的更好的方法来检索信息(如到目前为止,我可以判断它是否正常工作)?

enum DimensionType { Height, Width }
public static class DisplayInfo
{
    public static uint EffictivePixelHeight
    {
        get { return GetEffectivePixels(DimensionType.Height); }
    }

    public static uint EffectivePixelWidth
    {
        get { return GetEffectivePixels(DimensionType.Width); }
    }

    /// <summary>
    /// Calculate Effective Pixels based on RawPixelCount and ResolutionScale
    /// </summary>
    /// <param name="t">calculate width or height</param>
    /// <returns>0 if invalid, effecective pixel height/width based on t</returns>
    private static uint GetEffectivePixels(DimensionType t)
    {
        DisplayInformation info = DisplayInformation.GetForCurrentView();
        uint r = 0;
        switch (t)
        {
            case DimensionType.Height:
                r = info.ScreenHeightInRawPixels;
                break;
            case DimensionType.Width:
                r = info.ScreenWidthInRawPixels;
                break;
            default:
                break;
        }
        float sf = 0;
        switch (info.ResolutionScale)
        {
            case ResolutionScale.Invalid:
                sf = 0;
                break;
            case ResolutionScale.Scale100Percent:
                sf = 1;
                break;
            case ResolutionScale.Scale120Percent:
                sf = 1 / 1.2f;
                break;
            case ResolutionScale.Scale125Percent:
                sf = 1 / 1.25f;
                break;
            case ResolutionScale.Scale140Percent:
                sf = 1 / 1.4f;
                break;
            case ResolutionScale.Scale150Percent:
                sf = 1 / 1.5f;
                break;
            case ResolutionScale.Scale160Percent:
                sf = 1 / 1.6f;
                break;
            case ResolutionScale.Scale175Percent:
                sf = 1 / 1.75f;
                break;
            case ResolutionScale.Scale180Percent:
                sf = 1 / 1.8f;
                break;
            case ResolutionScale.Scale200Percent:
                sf = 1 / 2f;
                break;
            case ResolutionScale.Scale225Percent:
                sf = 1 / 2.25f;
                break;
            case ResolutionScale.Scale250Percent:
                sf = 1 / 2.5f;
                break;
            case ResolutionScale.Scale300Percent:
                sf = 1 / 3f;
                break;
            case ResolutionScale.Scale350Percent:
                sf = 1 / 3.5f;
                break;
            case ResolutionScale.Scale400Percent:
                sf = 1 / 4f;
                break;
            case ResolutionScale.Scale450Percent:
                sf = 1 / 4.5f;
                break;
            case ResolutionScale.Scale500Percent:
                sf = 1 / 5f;
                break;
            default:
                sf = 0;
                break;
        }

        r = (uint)(r * sf);

        return r;
    }
}

1 个答案:

答案 0 :(得分:1)

看起来你试图获得显示器/屏幕的有效Pixel尺寸的方式是正确的。但是你正在使用一定数量的开关分支来计算比例因子,这看起来非常乏味且不必要。实际上,您可能只需要一个代码行来计算比例因子。

double scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; 

因此,简化后,您的代码段可能如下:

enum DimensionType { Height, Width }
public static class DisplayInfo
{
   public static uint EffictivePixelHeight
   {
       get { return GetEffectivePixels(DimensionType.Height); }
   }

   public static uint EffectivePixelWidth
   {
       get { return GetEffectivePixels(DimensionType.Width); }
   }

   /// <summary>
   /// Calculate Effective Pixels based on RawPixelCount and ResolutionScale
   /// </summary>
   /// <param name="t">calculate width or height</param>
   /// <returns>0 if invalid, effecective pixel height/width based on t</returns>
   private static uint GetEffectivePixels(DimensionType t)
   {
       DisplayInformation info = DisplayInformation.GetForCurrentView();
       uint r = 0;
       switch (t)
       {
           case DimensionType.Height:
               r = info.ScreenHeightInRawPixels;
               break;
           case DimensionType.Width:
               r = info.ScreenWidthInRawPixels;
               break;
           default:
               break;
       } 
       double scaleFactor = info.RawPixelsPerViewPixel;               
       r = (uint)(r * (1 / scaleFactor));  
       return r;
   }
}

更多详情请参阅DisplayInformation课程。