Fix GetDeviceCoordinates. In some cases PresentationSource.FromVisual(this) may return null.

This commit is contained in:
hemn.still
2015-01-30 15:16:24 +04:00
parent d26ca4483f
commit 0f7f9778c1
3 changed files with 41 additions and 19 deletions

View File

@@ -0,0 +1,39 @@
using System;
using System.Windows.Interop;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public static class SystemInfo
{
private static Tuple<double, double> dpiFactors;
private static Tuple<double, double> DpiFactors
{
get
{
if (dpiFactors == null)
using (var source = new HwndSource(new HwndSourceParameters()))
dpiFactors = Tuple.Create(source.CompositionTarget.TransformToDevice.M11, source.CompositionTarget.TransformToDevice.M22);
return dpiFactors;
}
}
public static double DpiXFactor
{
get
{
var factors = DpiFactors;
return factors != null ? factors.Item1 : 1;
}
}
public static double DpiYFactor
{
get
{
var factors = DpiFactors;
return factors != null ? factors.Item2 : 1;
}
}
}
}