WPF acquisition system DPI variety of methods
due to the size of the WPF DPI units and associated systems, we sometimes need to get the DPI value to adjust some of the interface layout, this paper summarizes some programs in WPF method to obtain the system DPI.
First, define the following structures respectively stored component values of the X and Y directions, usually two values are identical.
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);
}
- 1
- 2
- 3
- 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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Graphics
add a reference to 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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
ManagementClass
the System.Management references
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
Considering in terms of cross-platform, multi-screen performance, we recommend CompositionTarget method. In addition, the monitoring system DPI change of method:
SystemEvents.DisplaySettingsChanged - SystemEvents Class
WM_DPICHANGED message
参考资料
Best way to get DPI value in WPF
How can I get the DPI in WPF?
————————————————
Disclaimer: This article is the original article CSDN bloggers "Iron_Ye", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/Iron_Ye/article/details/83053393