整理控件库
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
namespace WPFluent.Hardware;
|
||||
|
||||
/// <summary>
|
||||
/// Stores DPI information from which a <see cref="System.Windows.Media.Visual"/> or <see
|
||||
/// cref="System.Windows.UIElement"/> is rendered.
|
||||
/// </summary>
|
||||
public readonly struct DisplayDpi
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DisplayDpi"/> structure.
|
||||
/// </summary>
|
||||
/// <param name="dpiScaleX">The DPI scale on the X axis.</param>
|
||||
/// <param name="dpiScaleY">The DPI scale on the Y axis.</param>
|
||||
public DisplayDpi(double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
DpiScaleX = dpiScaleX;
|
||||
DpiScaleY = dpiScaleY;
|
||||
|
||||
DpiX = (int)Math.Round(DpiHelper.DefaultDpi * dpiScaleX, MidpointRounding.AwayFromZero);
|
||||
DpiY = (int)Math.Round(DpiHelper.DefaultDpi * dpiScaleY, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DisplayDpi"/> structure.
|
||||
/// </summary>
|
||||
/// <param name="dpiX">The DPI on the X axis.</param>
|
||||
/// <param name="dpiY">The DPI on the Y axis.</param>
|
||||
public DisplayDpi(int dpiX, int dpiY)
|
||||
{
|
||||
DpiX = dpiX;
|
||||
DpiY = dpiY;
|
||||
|
||||
DpiScaleX = dpiX / (double)DpiHelper.DefaultDpi;
|
||||
DpiScaleY = dpiY / (double)DpiHelper.DefaultDpi;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DPI scale on the X axis.
|
||||
/// </summary>
|
||||
public double DpiScaleX { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DPI scale on the Y axis.
|
||||
/// </summary>
|
||||
public double DpiScaleY { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DPI on the X axis.
|
||||
/// </summary>
|
||||
public int DpiX { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DPI on the Y axis.
|
||||
/// </summary>
|
||||
public int DpiY { get; }
|
||||
}
|
||||
@@ -17,12 +17,6 @@ internal static class DpiHelper
|
||||
/// </summary>
|
||||
internal const int DefaultDpi = 96;
|
||||
|
||||
[ThreadStatic]
|
||||
private static Matrix _transformToDevice;
|
||||
|
||||
[ThreadStatic]
|
||||
private static Matrix _transformToDip;
|
||||
|
||||
public static float ScaleX => GetScale().X;
|
||||
public static float ScaleY => GetScale().Y;
|
||||
|
||||
@@ -35,165 +29,4 @@ internal static class DpiHelper
|
||||
return new(scaleX / 96f, scaleY / 96f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a point in system coordinates to a point in device independent pixels (1/96").
|
||||
/// </summary>
|
||||
/// <returns>Returns the parameter converted to the device independent coordinate system.</returns>
|
||||
public static Point DevicePixelsToLogical(Point devicePoint, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
_transformToDip = Matrix.Identity;
|
||||
_transformToDip.Scale(1d / dpiScaleX, 1d / dpiScaleY);
|
||||
|
||||
return _transformToDip.Transform(devicePoint);
|
||||
}
|
||||
|
||||
public static Rect DeviceRectToLogical(Rect deviceRectangle, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point topLeft = DevicePixelsToLogical(
|
||||
new Point(deviceRectangle.Left, deviceRectangle.Top),
|
||||
dpiScaleX,
|
||||
dpiScaleY);
|
||||
Point bottomRight = DevicePixelsToLogical(
|
||||
new Point(deviceRectangle.Right, deviceRectangle.Bottom),
|
||||
dpiScaleX,
|
||||
dpiScaleY);
|
||||
|
||||
return new Rect(topLeft, bottomRight);
|
||||
}
|
||||
|
||||
public static Size DeviceSizeToLogical(Size deviceSize, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point pt = DevicePixelsToLogical(new Point(deviceSize.Width, deviceSize.Height), dpiScaleX, dpiScaleY);
|
||||
|
||||
return new Size(pt.X, pt.Y);
|
||||
}
|
||||
|
||||
// TODO: Look into utilizing preprocessor symbols for more functionality
|
||||
// ----
|
||||
// There is an opportunity to check against NET46 if we can use
|
||||
// VisualTreeHelper in this class. We are currently not utilizing
|
||||
// it because it is not available in .NET Framework 4.6 (available
|
||||
// starting 4.6.2). For now, there is no need to overcomplicate this
|
||||
// solution for some infrequent DPI calculations. However, if this
|
||||
// becomes more central to various implementations, we may want to
|
||||
// look into fleshing it out a bit further.
|
||||
// ----
|
||||
// Reference: https://docs.microsoft.com/en-us/dotnet/standard/frameworks
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DPI values from <see cref="SystemParameters"/>.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The DPI values from <see cref="SystemParameters"/>. If the property cannot be accessed, the default value <see
|
||||
/// langword="96"/> is returned.
|
||||
/// </returns>
|
||||
public static DisplayDpi GetSystemDpi()
|
||||
{
|
||||
System.Reflection.PropertyInfo? dpiXProperty = typeof(SystemParameters).GetProperty(
|
||||
"DpiX",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
||||
|
||||
if (dpiXProperty == null)
|
||||
{
|
||||
return new DisplayDpi(DefaultDpi, DefaultDpi);
|
||||
}
|
||||
|
||||
System.Reflection.PropertyInfo? dpiYProperty = typeof(SystemParameters).GetProperty(
|
||||
"Dpi",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
||||
|
||||
if (dpiYProperty == null)
|
||||
{
|
||||
return new DisplayDpi(DefaultDpi, DefaultDpi);
|
||||
}
|
||||
|
||||
return new DisplayDpi((int)dpiXProperty.GetValue(null, null)!, (int)dpiYProperty.GetValue(null, null)!);
|
||||
}
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// Occurs when application DPI is changed.
|
||||
/// </summary>
|
||||
public static event EventHandler<DpiChangedEventArgs> DpiChanged;
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// Gets DPI of the selected <see cref="Window"/>.
|
||||
/// </summary>
|
||||
/// <param name="window">The window that you want to get information about.</param>
|
||||
public static DisplayDpi GetWindowDpi(Window? window)
|
||||
{
|
||||
if (window is null)
|
||||
{
|
||||
return new DisplayDpi(DefaultDpi, DefaultDpi);
|
||||
}
|
||||
|
||||
return GetWindowDpi(new WindowInteropHelper(window).Handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets DPI of the selected <see cref="Window"/> based on it's handle.
|
||||
/// </summary>
|
||||
/// <param name="windowHandle">Handle of the window that you want to get information about.</param>
|
||||
public static DisplayDpi GetWindowDpi(IntPtr windowHandle)
|
||||
{
|
||||
if (windowHandle == IntPtr.Zero/* || !UnsafeNativeMethods.IsValidWindow(windowHandle)*/)
|
||||
{
|
||||
return new DisplayDpi(DefaultDpi, DefaultDpi);
|
||||
}
|
||||
|
||||
var windowDpi = (int)User32.GetDpiForWindow(windowHandle);
|
||||
|
||||
return new DisplayDpi(windowDpi, windowDpi);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a point in device independent pixels (1/96") to a point in the system coordinates.
|
||||
/// </summary>
|
||||
/// <param name="logicalPoint">A point in the logical coordinate system.</param>
|
||||
/// <param name="dpiScaleX">Horizontal DPI scale.</param>
|
||||
/// <param name="dpiScaleY">Vertical DPI scale.</param>
|
||||
/// <returns>Returns the parameter converted to the system's coordinates.</returns>
|
||||
public static Point LogicalPixelsToDevice(Point logicalPoint, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
_transformToDevice = Matrix.Identity;
|
||||
_transformToDevice.Scale(dpiScaleX, dpiScaleY);
|
||||
|
||||
return _transformToDevice.Transform(logicalPoint);
|
||||
}
|
||||
|
||||
public static Rect LogicalRectToDevice(Rect logicalRectangle, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point topLeft = LogicalPixelsToDevice(
|
||||
new Point(logicalRectangle.Left, logicalRectangle.Top),
|
||||
dpiScaleX,
|
||||
dpiScaleY);
|
||||
Point bottomRight = LogicalPixelsToDevice(
|
||||
new Point(logicalRectangle.Right, logicalRectangle.Bottom),
|
||||
dpiScaleX,
|
||||
dpiScaleY);
|
||||
|
||||
return new Rect(topLeft, bottomRight);
|
||||
}
|
||||
|
||||
public static Size LogicalSizeToDevice(Size logicalSize, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point pt = LogicalPixelsToDevice(new Point(logicalSize.Width, logicalSize.Height), dpiScaleX, dpiScaleY);
|
||||
|
||||
return new Size { Width = pt.X, Height = pt.Y };
|
||||
}
|
||||
|
||||
public static Thickness LogicalThicknessToDevice(Thickness logicalThickness, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point topLeft = LogicalPixelsToDevice(
|
||||
new Point(logicalThickness.Left, logicalThickness.Top),
|
||||
dpiScaleX,
|
||||
dpiScaleY);
|
||||
Point bottomRight = LogicalPixelsToDevice(
|
||||
new Point(logicalThickness.Right, logicalThickness.Bottom),
|
||||
dpiScaleX,
|
||||
dpiScaleY);
|
||||
|
||||
return new Thickness(topLeft.X, topLeft.Y, bottomRight.X, bottomRight.Y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
|
||||
|
||||
|
||||
namespace WPFluent.Hardware;
|
||||
|
||||
/// <summary>
|
||||
/// Set of tools for hardware acceleration.
|
||||
/// </summary>
|
||||
public static class HardwareAcceleration
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the provided rendering tier is supported.
|
||||
/// </summary>
|
||||
/// <param name="tier">Hardware acceleration rendering tier to check.</param>
|
||||
/// <returns><see langword="true"/> if tier is supported.</returns>
|
||||
public static bool IsSupported(RenderingTier tier) { return RenderCapability.Tier >> 16 >= (int)tier; }
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
|
||||
|
||||
|
||||
namespace WPFluent.Hardware;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="int"/> value whose high-order word corresponds to the rendering tier for the current thread.
|
||||
/// <para>Starting in the .NET Framework 4, rendering tier 1 has been redefined to only include graphics hardware that
|
||||
/// supports DirectX 9.0 or greater. Graphics hardware that supports DirectX 7 or 8 is now defined as rendering tier
|
||||
/// 0.</para>
|
||||
/// </summary>
|
||||
public enum RenderingTier
|
||||
{
|
||||
/// <summary>
|
||||
/// No graphics hardware acceleration is available for the application on the device. All graphics features use
|
||||
/// software acceleration. The DirectX version level is less than version 9.0.
|
||||
/// </summary>
|
||||
NoAcceleration = 0x0,
|
||||
|
||||
/// <summary>
|
||||
/// Most of the graphics features of WPF will use hardware acceleration if the necessary system resources are
|
||||
/// available and have not been exhausted. This corresponds to a DirectX version that is greater than or equal to
|
||||
/// 9.0.
|
||||
/// </summary>
|
||||
PartialAcceleration = 0x1,
|
||||
|
||||
/// <summary>
|
||||
/// Most of the graphics features of WPF will use hardware acceleration provided the necessary system resources have
|
||||
/// not been exhausted. This corresponds to a DirectX version that is greater than or equal to 9.0.
|
||||
/// </summary>
|
||||
FullAcceleration = 0x2,
|
||||
}
|
||||
Reference in New Issue
Block a user