原文: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;
}
}
 
  • 1
  • 2
  • 3
  • 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
  • 2
  • 3
  • 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);
}
 

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 变化的方法:

SystemEvents.DisplaySettingsChanged - SystemEvents Class
WM_DPICHANGED message
参考资料
Best way to get DPI value in WPF
How can I get the DPI in WPF?
————————————————

版权声明:本文为CSDN博主「Iron_Ye」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Iron_Ye/article/details/83053393

WPF 获取系统 DPI 的多种方法的更多相关文章

  1. Android获取系统时间的多种方法

    Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...

  2. PHP获取时间日期的多种方法

    分享下PHP获取时间日期的多种方法. <?php echo "今天:".date("Y-m-d")."<br>";     ...

  3. WPF 获取程序路径的一些方法,根据程序路径获取程序集信息

    一.WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.AppDomain.CurrentDomain.BaseDirector ...

  4. C# API 获取系统DPI缩放倍数跟分辨率大小

    原文:C# API 获取系统DPI缩放倍数跟分辨率大小 using System; using System.Drawing; using System.Runtime.InteropServices ...

  5. Shell获取字符串长度的多种方法总结

    摘自:https://www.jb51.net/article/121290.htm 前言 我们在日常工作中,对于求字符串操作在shell脚本中很常用,实现的方法有很多种,下面就来给大家归纳.汇总了求 ...

  6. 【Darwin学习笔记】之获取系统处理器数量的方法

    阅读Darwin源码的时候看到这个方法,感觉挺有用处,且考虑了多种平台下的实现方式,直接贴代码,以后说不定会用到~ 单一种平台下的实现方法可能很容易,但是把这些个系统都收集在一起,在一个函数中实现还是 ...

  7. 获取系统DPI、系统显示比例等

    using System; using System.Drawing; using System.Runtime.InteropServices; namespace XYDES { public c ...

  8. WPF获取程序版本号(Version)的方法

    1.第一种:通过System来获取 public static Version GetEdition() { return System.Reflection.Assembly.GetExecutin ...

  9. WPF实现系统禁音的方法

    方法1: [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern ...

随机推荐

  1. SpringMVC框架之第四篇

    5.SpringMVC异常处理 5.1.异常分类 1.可预知异常: Java编译时可检测异常,例如:IOException.SQLException等. 自定义异常(继承Exception父类的自定义 ...

  2. How to: Implement File Data Properties 如何:实现文件数据属性

    This topic demonstrates how to implement a business class with a file data property and a file colle ...

  3. Auto入门 之 常用概念

    1.SEMI (Semiconductor Equipment And Materials International)  国际半导体设备与材料产业协会 2.SECS SECS协议是基于RS-232或 ...

  4. LRC歌词原理和实现高仿Android网易云音乐

    大家好,我们是爱学啊,今天给大家带来一篇关于LRC歌词原理和在Android上如何实现歌词逐行滚动的效果,本文来自[Android开发项目实战我的云音乐]课程:逐字滚动下一篇文章讲解. 效果图 相信大 ...

  5. 升级python2.7至python3.7

    最近在centos7下执行命令时,出现以下提示: DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020 ...

  6. screen工具的安装与使用

    yum install screen    安装screen screen -S <作业名称>     创建新的页 screen -ls   查询已经存在的页面 screen -r < ...

  7. InnoDB On-Disk Structures(三)--Tablespaces (转载)

    转载.节选于 https://dev.mysql.com/doc/refman/8.0/en/innodb-tablespace.html This section covers topics rel ...

  8. dnf & yum

    CentOS8 配置软件源 在 CentOS8 中.使用了基于DNF技术(YUM v4)的 YUM 工具. YUM v4 与之前在 CentOS7 上使用的 YUM v3 相比具有以下优点: 提高性能 ...

  9. python-将一个列表切分成多个小列表

    list是python中较为常见的数据类型,它是一个可迭代对象,迭代是什么?简单的可以理解成:一个可以被for循环遍历的对象 今天拿到一个类似这样的list list_info = ['name zh ...

  10. Spring 框架基础(02):Bean的生命周期,作用域,装配总结

    本文源码:GitHub·点这里 || GitEE·点这里 一.装配方式 Bean的概念:Spring框架管理的应用程序中,由Spring容器负责创建,装配,设置属性,进而管理整个生命周期的对象,称为B ...