为了账号安全,请及时绑定邮箱和手机立即绑定

如何在WPF中获取DPI?

/ 猿问

如何在WPF中获取DPI?

慕神8447489 2019-11-29 11:07:18

如何在WPF中获取DPI?



查看完整描述

3 回答

?
阿波罗的战车

http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx似乎有效


PresentationSource source = PresentationSource.FromVisual(this);


double dpiX, dpiY;

if (source != null) {

    dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;

    dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;

}


查看完整回答
反对 回复 2019-11-29
?
MMTTMM

var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);

var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);


var dpiX = (int)dpiXProperty.GetValue(null, null);

var dpiY = (int)dpiYProperty.GetValue(null, null);


查看完整回答
反对 回复 2019-11-29
?
动漫人物

我发现获得“真实”显示器dpi的唯一方法如下。所有其他提到的技术都只说96,这对于大多数显示器来说是不正确的。


 public class ScreenInformations

{

    public static uint RawDpi { get; private set; }


    static ScreenInformations()

    {

        uint dpiX;

        uint dpiY;

        GetDpi(DpiType.RAW, out dpiX, out dpiY);

        RawDpi = dpiX;

    }


    /// <summary>

    /// Returns the scaling of the given screen.

    /// </summary>

    /// <param name="dpiType">The type of dpi that should be given back..</param>

    /// <param name="dpiX">Gives the horizontal scaling back (in dpi).</param>

    /// <param name="dpiY">Gives the vertical scaling back (in dpi).</param>

    private static void GetDpi(DpiType dpiType, out uint dpiX, out uint dpiY)

    {

        var point = new System.Drawing.Point(1, 1);

        var hmonitor = MonitorFromPoint(point, _MONITOR_DEFAULTTONEAREST);


        switch (GetDpiForMonitor(hmonitor, dpiType, out dpiX, out dpiY).ToInt32())

        {

            case _S_OK: return;

            case _E_INVALIDARG:

                throw new ArgumentException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");

            default:

                throw new COMException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");

        }

    }


    //https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062.aspx

    [DllImport("User32.dll")]

    private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags);


    //https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx

    [DllImport("Shcore.dll")]

    private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY);


    const int _S_OK = 0;

    const int _MONITOR_DEFAULTTONEAREST = 2;

    const int _E_INVALIDARG = -2147024809;

}


/// <summary>

/// Represents the different types of scaling.

/// </summary>

/// <seealso cref="https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511.aspx"/>

public enum DpiType

{

    EFFECTIVE = 0,

    ANGULAR = 1,

    RAW = 2,

}


查看完整回答
反对 回复 2019-11-29

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信