From 0f7f9778c16d459a487dda0f476792991a92f816 Mon Sep 17 00:00:00 2001 From: "hemn.still" Date: Fri, 30 Jan 2015 15:16:24 +0400 Subject: [PATCH] Fix GetDeviceCoordinates. In some cases PresentationSource.FromVisual(this) may return null. --- .../NotifyIconWpf/Interop/SystemInfo.cs | 39 +++++++++++++++++++ .../Source/NotifyIconWpf/NotifyIconWpf.csproj | 1 + .../Source/NotifyIconWpf/TaskbarIcon.cs | 20 +--------- 3 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/Interop/SystemInfo.cs diff --git a/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/Interop/SystemInfo.cs b/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/Interop/SystemInfo.cs new file mode 100644 index 0000000..8274196 --- /dev/null +++ b/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/Interop/SystemInfo.cs @@ -0,0 +1,39 @@ +using System; +using System.Windows.Interop; + +namespace Hardcodet.Wpf.TaskbarNotification.Interop +{ + public static class SystemInfo + { + private static Tuple dpiFactors; + + private static Tuple 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; + } + } + } +} diff --git a/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/NotifyIconWpf.csproj b/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/NotifyIconWpf.csproj index 981fc5e..4e7a2af 100644 --- a/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/NotifyIconWpf.csproj +++ b/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/NotifyIconWpf.csproj @@ -47,6 +47,7 @@ + Code diff --git a/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/TaskbarIcon.cs b/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/TaskbarIcon.cs index 884eedc..e59a5d5 100644 --- a/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/TaskbarIcon.cs +++ b/Hardcodet.NotifyIcon.Wpf/Source/NotifyIconWpf/TaskbarIcon.cs @@ -962,25 +962,7 @@ namespace Hardcodet.Wpf.TaskbarNotification /// private Point GetDeviceCoordinates(Point point) { - if (double.IsNaN(scalingFactor)) - { - //calculate scaling factor in order to support non-standard DPIs - var presentationSource = PresentationSource.FromVisual(this); - if (presentationSource == null) - { - scalingFactor = 1; - } - else - { - var transform = presentationSource.CompositionTarget.TransformToDevice; - scalingFactor = 1/transform.M11; - } - } - - //on standard DPI settings, just return the point - if (scalingFactor == 1.0) return point; - - return new Point() {X = (int) (point.X*scalingFactor), Y = (int) (point.Y*scalingFactor)}; + return new Point() { X = (int)(point.X / SystemInfo.DpiXFactor), Y = (int)(point.Y / SystemInfo.DpiYFactor) }; } #region Dispose / Exit