시스템 DPI의 다양한 방법을 얻을 WPF

원본 : WPF는 시스템 DPI의 다양한 방법을 얻을

방법의 WPF 수집 시스템 DPI의 다양한
인해 WPF DPI 단위 및 관련 시스템의 크기는, 우리가 때때로 인터페이스 레이아웃의 일부를 조정할 수있는 DPI 값을 얻을 필요가,이 논문은 시스템 DPI를 얻기 위해 WPF 방법에서 일부 프로그램을 요약 한 것입니다.

먼저, 각각 X 및 Y 방향의 성분 값을 저장하기 구조를 정의하는, 일반적으로 두 값이 동일하다.

public struct Dpi
{
    public double X { get; set; }

    public double Y { get; set; }

    public Dpi(double x, double y)
    {
        X = x;
        Y = y;
    }
}

   
   
  
  
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)
  • (12)

CompositionTarget

public static Dpi GetDpiFromVisual(Visual visual)
{
    var source = PresentationSource.FromVisual(visual);

    var dpiX = 96.0;
    var dpiY = 96.0;

    if (source?.CompositionTarget != null)
    {
        dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
        dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;
    }

    return new Dpi(dpiX, dpiY);
}

   
   
  
  
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)
  • (12)
  • (13)
  • (14)
  • (15)

Win32 API를

private const int LOGPIXELSX = 88;
private const int LOGPIXELSY = 90;

[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int index);

[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);

public static Dpi GetDpiByWin32()
{
    var hDc = GetDC(IntPtr.Zero);

    var dpiX = GetDeviceCaps(hDc, LOGPIXELSX);
    var dpiY = GetDeviceCaps(hDc, LOGPIXELSY);

    ReleaseDC(IntPtr.Zero, hDc);
    return new Dpi(dpiX, dpiY);
}

   
   
  
  
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)
  • (12)
  • (13)
  • (14)
  • (15)
  • (16)
  • (17)
  • (18)
  • (19)
  • (20)
  • (21)
  • (22)

SystemParameters

public static Dpi GetDpiBySystemParameters()
{
    const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Static;

    var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", bindingFlags);
    var dpiYProperty = typeof(SystemParameters).GetProperty("DpiY", bindingFlags);

    var dpiX = 96.0;
    var dpiY = 96.0;

    if (dpiXProperty != null)
    {
        dpiX = (double)dpiXProperty.GetValue(null, null);
    }

    if (dpiYProperty != null)
    {
        dpiY = (double)dpiYProperty.GetValue(null, null);
    }

    return new Dpi(dpiX, dpiY);
}

   
   
  
  
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)
  • (12)
  • (13)
  • (14)
  • (15)
  • (16)
  • (17)
  • (18)
  • (19)
  • (20)
  • (21)
  • (22)

그래픽
System.Drawing에 대한 참조를 추가

public static Dpi GetDpiByGraphics()
{
    double dpiX;
    double dpiY;

    using (var graphics = Graphics.FromHwnd(IntPtr.Zero))
    {
        dpiX = graphics.DpiX;
        dpiY = graphics.DpiY;
    }

    return new Dpi(dpiX, dpiY);
}

   
   
  
  
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)
  • (12)
  • (13)

수 managementclass
System.Management 참조를

public static Dpi GetDpiByManagement()
{
    var dpiX = 96.0;
    var dpiY = 96.0;

    using (var mc = new ManagementClass("Win32_DesktopMonitor"))
    {
        using (var moc = mc.GetInstances())
        {
            // there may be many, to filter the ones you are interested in
            foreach (var mo in moc)
            {
                dpiX = double.Parse(mo.Properties["PixelsPerXLogicalInch"].Value.ToString());
                dpiY = double.Parse(mo.Properties["PixelsPerYLogicalInch"].Value.ToString());
            }
        }
    }

    return new Dpi(dpiX, dpiY);
}

   
   
  
  
  • 1
  • 4
  • 5
  • 6
  • (7)
  • 8
  • 9
  • (10)
  • (11)
  • (12)
  • (13)
  • (14)
  • (15)
  • (16)
  • (17)
  • (18)
  • (19)
  • (20)

크로스 플랫폼의 측면에서 고려할 때, 멀티 스크린 성능, 우리는 CompositionTarget 방법을 권장합니다. 또한, 방법의 모니터링 시스템 DPI 변화율 :

SystemEvents.DisplaySettingsChanged - SystemEvents 클래스
WM_DPICHANGED 메시지
参考资料
WPF에서 DPI 값을 얻을 수있는 가장 좋은 방법은
내가 WPF에서 DPI를 얻을 수 있습니까?
----------------

면책 조항 :이 문서에 원래 문서 CSDN 블로거 "Iron_Ye"및 후속의 CC 4.0-SA 저작권 계약, 원본 소스 링크이 문을 첨부 해주세요 재현이다.
원본 링크 : HTTPS : //blog.csdn.net/Iron_Ye/article/details/83053393

출시 세 원저 · 원의 칭찬 0 · 조회수 968

추천

출처www.cnblogs.com/lonelyxmas/p/12075358.html