replace v6 with experimental v7 code
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
public static class VisualHelper
|
||||
{
|
||||
internal static VisualStateGroup TryGetVisualStateGroup(DependencyObject d, string groupName)
|
||||
{
|
||||
var root = GetImplementationRoot(d);
|
||||
if (root == null) return null;
|
||||
|
||||
return VisualStateManager
|
||||
.GetVisualStateGroups(root)?
|
||||
.OfType<VisualStateGroup>()
|
||||
.FirstOrDefault(group => string.CompareOrdinal(groupName, group.Name) == 0);
|
||||
}
|
||||
|
||||
internal static FrameworkElement GetImplementationRoot(DependencyObject d) =>
|
||||
1 == VisualTreeHelper.GetChildrenCount(d)
|
||||
? VisualTreeHelper.GetChild(d, 0) as FrameworkElement
|
||||
: null;
|
||||
|
||||
public static T GetChild<T>(DependencyObject d) where T : DependencyObject
|
||||
{
|
||||
if (d == null) return default;
|
||||
if (d is T t) return t;
|
||||
|
||||
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(d, i);
|
||||
|
||||
var result = GetChild<T>(child);
|
||||
if (result != null) return result;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public static T GetParent<T>(DependencyObject d) where T : DependencyObject
|
||||
{
|
||||
if (d == null)
|
||||
return default;
|
||||
|
||||
if (d is T)
|
||||
return d as T;
|
||||
|
||||
if (d is Window)
|
||||
return null;
|
||||
|
||||
return GetParent<T>(VisualTreeHelper.GetParent(d));
|
||||
}
|
||||
|
||||
public static IntPtr GetHandle(this Visual visual) => (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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user