Added Pixel to Device Independent Pixel transformation to WindowIntelopHelper

MainWindow GetWindowsTop and GetWindowsLeft will use DIP when searching for the center of the screen
This commit is contained in:
Boris Makogonyuk
2015-12-13 04:22:01 +01:00
parent bb9684f7d3
commit 83985aa44c
2 changed files with 38 additions and 21 deletions

View File

@@ -4,6 +4,8 @@ using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media;
using Point = System.Windows.Point;
namespace Wox.Helper
{
@@ -80,6 +82,32 @@ namespace Wox.Helper
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
/// <summary>
/// Transforms pixels to Device Independent Pixels used by WPF
/// </summary>
/// <param name="visual">current window, required to get presentation source</param>
/// <param name="unitX">horizontal position in pixels</param>
/// <param name="unitY">vertical position in pixels</param>
/// <returns>point containing device independent pixels</returns>
public static Point TransformPixelsToDIP(Visual visual, double unitX, double unitY)
{
Matrix matrix;
var source = PresentationSource.FromVisual(visual);
if (source != null)
{
matrix = source.CompositionTarget.TransformFromDevice;
}
else
{
using (var src = new HwndSource(new HwndSourceParameters()))
{
matrix = src.CompositionTarget.TransformFromDevice;
}
}
return new Point((int) (matrix.M11*unitX), (int) (matrix.M22*unitY));
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{