WPF 获取系统 DPI 的多种方法

WPF 获取系统 DPI 的多种方法

由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 WPF 程序中获取系统 DPI 的方法。

首先,定义如下结构体来分别保存 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;
    }
}

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);
}

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);
}

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);
}

Graphics

添加 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);
}

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);
}

处于 跨平台、多屏幕、性能 等方面的综合考虑,推荐使用 CompositionTarget 方法。另外,监听系统 DPI 变化的方法:

参考资料

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ironyho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值