public static POINT GetCursorPosition() { POINT currentMousePoint; var gotPoint = GetCursorPos(out currentMousePoint); if (!gotPoint) { currentMousePoint = new POINT(0, 0); } return currentMousePoint; }
private static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);
private static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
private static extern int ScreenToClient(IntPtr hwnd, ref POINT pt);
private static extern void GetCursorPos(out POINT pt);
private static extern bool GetCursorPos(out POINT lpPoint);
internal static extern bool GetCursorPos(out POINT lpPoint);
/// <summary> /// This converts the position into screen coords /// </summary> /// <remarks> /// Got this here: /// http://blogs.msdn.com/llobo/archive/2006/05/02/Code-for-getting-screen-relative-Position-in-WPF.aspx /// </remarks> public static Point TransformToScreen(Point point, Visual relativeTo) { HwndSource hwndSource = PresentationSource.FromVisual(relativeTo) as HwndSource; Visual root = hwndSource.RootVisual; // Translate the point from the visual to the root. GeneralTransform transformToRoot = relativeTo.TransformToAncestor(root); Point pointRoot = transformToRoot.Transform(point); // Transform the point from the root to client coordinates. Matrix m = Matrix.Identity; Transform transform = VisualTreeHelper.GetTransform(root); if (transform != null) { m = Matrix.Multiply(m, transform.Value); } Vector offset = VisualTreeHelper.GetOffset(root); m.Translate(offset.X, offset.Y); Point pointClient = m.Transform(pointRoot); // Convert from “device-independent pixels” into pixels. pointClient = hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient); POINT pointClientPixels = new POINT(); pointClientPixels.x = (0 < pointClient.X) ? (int)(pointClient.X + 0.5) : (int)(pointClient.X - 0.5); pointClientPixels.y = (0 < pointClient.Y) ? (int)(pointClient.Y + 0.5) : (int)(pointClient.Y - 0.5); // Transform the point into screen coordinates. POINT pointScreenPixels = pointClientPixels; ClientToScreen(hwndSource.Handle, pointScreenPixels); return new Point(pointScreenPixels.x, pointScreenPixels.y); }
static extern int SetAdapter(POINT screenSpacePoint);
public static extern bool GetCursorPos(ref POINT lpPoint);
static extern IntPtr MonitorFromPoint(POINT pt, int dwFlags);
private static extern bool GetCursorPos(out POINT pt);
private static extern IntPtr MonitorFromPoint(POINT pt, MonitorOptions dwFlags);
private static extern IntPtr WindowFromPoint(POINT Point);
public static extern bool GetCursorPos(out POINT pt);
static extern void SetCamera(POINT pos);
static extern bool GetCursorPos(out POINT lpMousePoint);
void AdapterTimer_Tick(object sender, EventArgs e) { POINT p = new POINT(imgelt.PointToScreen(new Point(0, 0))); HRESULT.Check(SetAdapter(p)); }
public static bool MoveCursor(POINT point) { return SetCursorPos(point.x, point.y); }
protected override void OnPreviewKeyDown(KeyEventArgs e) { base.OnPreviewKeyDown(e); if ((!IsMouseCaptured) || (e.Key != Key.H)) return; e.Handled = true; var pos = PointToScreen(Mouse.GetPosition(this)); var point = new POINT { X = (int)pos.X, Y = (int)pos.Y }; var hwnd = WindowFromPoint(point); if (hwnd == new WindowInteropHelper(this).Handle) { var points = new List<Point> { new Point(Owner.Left + 10, Owner.Top + 10), // Top left new Point(Owner.Left + Owner.ActualWidth - ActualWidth - 10, Owner.Top + 10), // Top right new Point(Owner.Left + 10, Owner.Top + Owner.ActualHeight - ActualHeight - 10), // Bottom left new Point(Owner.Left - 10 + Owner.ActualWidth - ActualWidth, Owner.Top + Owner.ActualHeight - ActualHeight - 10), // Bottom right }; var current = new Point(Left, Top); var furthestPoint = points.OrderByDescending(testPoint => (testPoint - current).LengthSquared).First(); Left = furthestPoint.X; Top = furthestPoint.Y; return; } hwnd = GetAncestor(hwnd, GA_ROOT); if (hwnd == GetDesktopWindow()) return; ShowWindow(hwnd, SW_HIDE); hidden.Add(hwnd); }