Files
community/Ink Canvas/Helpers/IsOutsideOfScreenHelper.cs
T

34 lines
1.4 KiB
C#
Raw Normal View History

2025-07-28 14:40:44 +08:00
using System.Drawing;
using System.Linq;
2025-05-25 09:29:48 +08:00
using System.Windows;
2025-07-28 14:40:44 +08:00
using System.Windows.Forms;
using System.Windows.Interop;
using Point = System.Windows.Point;
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-07-28 14:40:44 +08:00
var screens = Screen.AllScreens;
2025-05-25 09:29:48 +08:00
return !screens.Any(x => x.Bounds.IntersectsWith(targetBounds));
2025-07-28 14:40:44 +08:00
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-07-28 14:40:44 +08:00
return new Rectangle(
2025-05-25 09:29:48 +08:00
(int)pixelBoundsToScreen.X, (int)pixelBoundsToScreen.Y,
(int)pixelBoundsToScreen.Width, (int)pixelBoundsToScreen.Height);
}
}
}
}