add function codes
This commit is contained in:
1 parent
cae862d6b0
commit
c1d6e901b2
31 files changed
+2428
-169
No files matched your search
@@ -1,14 +1,54 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
internal class DpiHelper
|
||||
internal static class DpiHelper
|
||||
{
|
||||
[ThreadStatic]
|
||||
private const double LogicalDpi = 96.0;
|
||||
|
||||
[ThreadStatic]
|
||||
private static Matrix _transformToDip;
|
||||
|
||||
static DpiHelper()
|
||||
{
|
||||
var dC = InteropMethods.GetDC(IntPtr.Zero);
|
||||
if (dC != IntPtr.Zero)
|
||||
{
|
||||
DeviceDpiX = InteropMethods.GetDeviceCaps(dC, 88);
|
||||
DeviceDpiY = InteropMethods.GetDeviceCaps(dC, 90);
|
||||
InteropMethods.ReleaseDC(IntPtr.Zero, dC);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeviceDpiX = LogicalDpi;
|
||||
DeviceDpiY = LogicalDpi;
|
||||
}
|
||||
|
||||
var identity = Matrix.Identity;
|
||||
var identity2 = Matrix.Identity;
|
||||
identity.Scale(DeviceDpiX / LogicalDpi, DeviceDpiY / LogicalDpi);
|
||||
identity2.Scale(LogicalDpi / DeviceDpiX, LogicalDpi / DeviceDpiY);
|
||||
TransformFromDevice = new MatrixTransform(identity2);
|
||||
TransformFromDevice.Freeze();
|
||||
TransformToDevice = new MatrixTransform(identity);
|
||||
TransformToDevice.Freeze();
|
||||
}
|
||||
|
||||
public static MatrixTransform TransformFromDevice { get; }
|
||||
|
||||
public static MatrixTransform TransformToDevice { get; }
|
||||
|
||||
public static double DeviceDpiX { get; }
|
||||
|
||||
public static double DeviceDpiY { get; }
|
||||
|
||||
public static double LogicalToDeviceUnitsScalingFactorX => TransformToDevice.Matrix.M11;
|
||||
|
||||
public static double LogicalToDeviceUnitsScalingFactorY => TransformToDevice.Matrix.M22;
|
||||
|
||||
public static Point DevicePixelsToLogical(Point devicePoint, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
_transformToDip = Matrix.Identity;
|
||||
@@ -18,9 +58,22 @@ namespace HandyControl.Tools
|
||||
|
||||
public static Size DeviceSizeToLogical(Size deviceSize, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point pt = DevicePixelsToLogical(new Point(deviceSize.Width, deviceSize.Height), dpiScaleX, dpiScaleY);
|
||||
|
||||
var pt = DevicePixelsToLogical(new Point(deviceSize.Width, deviceSize.Height), dpiScaleX, dpiScaleY);
|
||||
return new Size(pt.X, pt.Y);
|
||||
}
|
||||
|
||||
public static Rect LogicalToDeviceUnits(this Rect logicalRect)
|
||||
{
|
||||
var result = logicalRect;
|
||||
result.Transform(TransformToDevice.Matrix);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Rect DeviceToLogicalUnits(this Rect deviceRect)
|
||||
{
|
||||
var result = deviceRect;
|
||||
result.Transform(TransformFromDevice.Matrix);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user