Files

35 lines
1.5 KiB
C#
Raw Permalink Normal View History

2025-08-23 21:39:21 +08:00
using System;
using System.Collections.Generic;
2025-07-28 14:40:44 +08:00
using System.Linq;
2025-08-23 21:39:21 +08:00
using System.Text;
using System.Threading.Tasks;
2025-07-28 14:40:44 +08:00
using System.Windows.Interop;
2025-08-23 21:39:21 +08:00
using System.Windows;
2025-05-25 09:29:48 +08:00
namespace Ink_Canvas.Helpers {
internal class IsOutsideOfScreenHelper {
public static bool IsOutsideOfScreen(FrameworkElement target) {
var hwndSource = (HwndSource)PresentationSource.FromVisual(target);
if (hwndSource is null) {
return true;
}
var hWnd = hwndSource.Handle;
var targetBounds = GetPixelBoundsToScreen(target);
2025-08-23 21:39:21 +08:00
var screens = System.Windows.Forms.Screen.AllScreens;
2025-05-25 09:29:48 +08:00
return !screens.Any(x => x.Bounds.IntersectsWith(targetBounds));
2025-08-23 21:39:21 +08:00
System.Drawing.Rectangle GetPixelBoundsToScreen(FrameworkElement visual) {
2025-05-25 09:29:48 +08:00
var pixelBoundsToScreen = Rect.Empty;
pixelBoundsToScreen.Union(visual.PointToScreen(new Point(0, 0)));
pixelBoundsToScreen.Union(visual.PointToScreen(new Point(visual.ActualWidth, 0)));
pixelBoundsToScreen.Union(visual.PointToScreen(new Point(0, visual.ActualHeight)));
pixelBoundsToScreen.Union(visual.PointToScreen(new Point(visual.ActualWidth, visual.ActualHeight)));
2025-08-23 21:39:21 +08:00
return new System.Drawing.Rectangle(
2025-05-25 09:29:48 +08:00
(int)pixelBoundsToScreen.X, (int)pixelBoundsToScreen.Y,
(int)pixelBoundsToScreen.Width, (int)pixelBoundsToScreen.Height);
}
}
}
}