|
|
using System.Windows; |
|
|
|
|
|
namespace ScreenGrab.Utilities; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class CursorClipper |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static bool ClipCursor(FrameworkElement element) |
|
|
{ |
|
|
const double dpi96 = 96.0; |
|
|
|
|
|
var topLeft = element.PointToScreen(new Point(0, 0)); |
|
|
|
|
|
var source = PresentationSource.FromVisual(element); |
|
|
if (source?.CompositionTarget == null) return false; |
|
|
|
|
|
var dpiX = dpi96 * source.CompositionTarget.TransformToDevice.M11; |
|
|
var dpiY = dpi96 * source.CompositionTarget.TransformToDevice.M22; |
|
|
|
|
|
var width = (int)((element.ActualWidth + 1) * dpiX / dpi96); |
|
|
var height = (int)((element.ActualHeight + 1) * dpiY / dpi96); |
|
|
|
|
|
var rect = new OSInterop.RECT |
|
|
{ |
|
|
left = (int)topLeft.X, |
|
|
top = (int)topLeft.Y, |
|
|
right = (int)topLeft.X + width, |
|
|
bottom = (int)topLeft.Y + height |
|
|
}; |
|
|
|
|
|
return OSInterop.ClipCursor(ref rect); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static bool UnClipCursor() |
|
|
{ |
|
|
return OSInterop.ClipCursor(IntPtr.Zero); |
|
|
} |
|
|
} |