forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDpiHelper.cs
More file actions
50 lines (40 loc) · 1.59 KB
/
Copy pathDpiHelper.cs
File metadata and controls
50 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Reflection;
using System.Windows;
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf
{
internal static class DpiHelper
{
private static readonly int DpiX;
private static readonly int DpiY;
private const double StandardDpiX = 96.0;
private const double StandardDpiY = 96.0;
static DpiHelper()
{
var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);
DpiX = (int)dpiXProperty.GetValue(null, null);
DpiY = (int)dpiYProperty.GetValue(null, null);
}
public static double TransformToDeviceY(Visual visual, double y)
{
var source = PresentationSource.FromVisual(visual);
if (source?.CompositionTarget != null) return y * source.CompositionTarget.TransformToDevice.M22;
return TransformToDeviceY(y);
}
public static double TransformToDeviceX(Visual visual, double x)
{
var source = PresentationSource.FromVisual(visual);
if (source?.CompositionTarget != null) return x * source.CompositionTarget.TransformToDevice.M11;
return TransformToDeviceX(x);
}
public static double TransformToDeviceY(double y)
{
return y * DpiY / StandardDpiY;
}
public static double TransformToDeviceX(double x)
{
return x * DpiX / StandardDpiX;
}
}
}