add function codes

This commit is contained in:
NaBian committed 2020-01-18 14:23:47 +08:00
1 parent cae862d6b0
commit c1d6e901b2
31 files changed
+2428 -169

No files matched your search

@@ -52,6 +52,15 @@ namespace HandyControl.Tools
SetLang(config.Lang);
}
public void SetWindowDefaultStyle(object resourceKey = null)
{
var metadata = resourceKey == null
? new FrameworkPropertyMetadata(Application.Current.FindResource(typeof(System.Windows.Window)))
: new FrameworkPropertyMetadata(Application.Current.FindResource(resourceKey));
FrameworkElement.StyleProperty.OverrideMetadata(typeof(System.Windows.Window), metadata);
}
public event PropertyChangedEventHandler PropertyChanged;
#if netle40
@@ -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;
}
}
}
@@ -81,7 +81,7 @@ namespace HandyControl.Tools
{
var bi = new InteropValues.BITMAPINFO(width, -height, 32)
{
bmiHeader_biCompression = InteropValues.BI_RGB
biCompression = InteropValues.BI_RGB
};
var bits = IntPtr.Zero;
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using HandyControl.Tools.Interop;
namespace HandyControl.Tools
{
internal class ScreenHelper
{
internal static void FindMaximumSingleMonitorRectangle(Rect windowRect, out Rect screenSubRect, out Rect monitorRect)
{
var windowRect2 = new InteropValues.RECT(windowRect);
FindMaximumSingleMonitorRectangle(windowRect2, out var rect, out var rect2);
screenSubRect = new Rect(rect.Position, rect.Size);
monitorRect = new Rect(rect2.Position, rect2.Size);
}
private static void FindMaximumSingleMonitorRectangle(InteropValues.RECT windowRect, out InteropValues.RECT screenSubRect, out InteropValues.RECT monitorRect)
{
var rects = new List<InteropValues.RECT>();
InteropMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref InteropValues.RECT rect, IntPtr lpData)
{
var monitorInfo = default(InteropValues.MONITORINFO);
monitorInfo.cbSize = (uint) Marshal.SizeOf(typeof(InteropValues.MONITORINFO));
InteropMethods.GetMonitorInfo(hMonitor, ref monitorInfo);
rects.Add(monitorInfo.rcWork);
return true;
}, IntPtr.Zero);
var num = 0L;
screenSubRect = new InteropValues.RECT
{
Left = 0,
Right = 0,
Top = 0,
Bottom = 0
};
monitorRect = new InteropValues.RECT
{
Left = 0,
Right = 0,
Top = 0,
Bottom = 0
};
foreach (var current in rects)
{
var rect = current;
InteropMethods.IntersectRect(out var rECT2, ref rect, ref windowRect);
var num2 = (long) (rECT2.Width * rECT2.Height);
if (num2 > num)
{
screenSubRect = rECT2;
monitorRect = current;
num = num2;
}
}
}
internal static void FindMonitorRectsFromPoint(Point point, out Rect monitorRect, out Rect workAreaRect)
{
var intPtr = InteropMethods.MonitorFromPoint(new InteropValues.POINT
{
X = (int) point.X,
Y = (int) point.Y
}, 2);
monitorRect = new Rect(0.0, 0.0, 0.0, 0.0);
workAreaRect = new Rect(0.0, 0.0, 0.0, 0.0);
if (intPtr != IntPtr.Zero)
{
InteropValues.MONITORINFO monitorInfo = default;
monitorInfo.cbSize = (uint) Marshal.SizeOf(typeof(InteropValues.MONITORINFO));
InteropMethods.GetMonitorInfo(intPtr, ref monitorInfo);
monitorRect = new Rect(monitorInfo.rcMonitor.Position, monitorInfo.rcMonitor.Size);
workAreaRect = new Rect(monitorInfo.rcWork.Position, monitorInfo.rcWork.Size);
}
}
}
}
@@ -3,6 +3,7 @@ using System.Linq;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using HandyControl.Tools.Interop;
namespace HandyControl.Tools
{
@@ -11,10 +12,7 @@ namespace HandyControl.Tools
internal static VisualStateGroup TryGetVisualStateGroup(DependencyObject d, string groupName)
{
var root = GetImplementationRoot(d);
if (root == null)
{
return null;
}
if (root == null) return null;
return VisualStateManager
.GetVisualStateGroups(root)?
@@ -31,10 +29,7 @@ namespace HandyControl.Tools
public static T GetChild<T>(DependencyObject d) where T : DependencyObject
{
if (d is T t)
{
return t;
}
if (d is T t) return t;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
{
@@ -47,7 +42,34 @@ namespace HandyControl.Tools
return default;
}
public static IntPtr GetHandle(this Visual visual) =>
(PresentationSource.FromVisual(visual) as HwndSource)?.Handle ?? IntPtr.Zero;
public static IntPtr GetHandle(this Visual visual)
{
return (PresentationSource.FromVisual(visual) as HwndSource)?.Handle ?? IntPtr.Zero;
}
internal static void HitTestVisibleElements(Visual visual, HitTestResultCallback resultCallback,
HitTestParameters parameters)
{
VisualTreeHelper.HitTest(visual, ExcludeNonVisualElements, resultCallback, parameters);
}
private static HitTestFilterBehavior ExcludeNonVisualElements(DependencyObject potentialHitTestTarget)
{
if (!(potentialHitTestTarget is Visual)) return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
if (!(potentialHitTestTarget is UIElement uIElement) || uIElement.IsVisible && uIElement.IsEnabled)
return HitTestFilterBehavior.Continue;
return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
}
internal static bool ModifyStyle(IntPtr hWnd, int styleToRemove, int styleToAdd)
{
var windowLong = InteropMethods.GetWindowLong(hWnd, InteropValues.GWL.STYLE);
var num = (windowLong & ~styleToRemove) | styleToAdd;
if (num == windowLong) return false;
InteropMethods.SetWindowLong(hWnd, InteropValues.GWL.STYLE, num);
return true;
}
}
}