internal static Point ScreenToClient(Point ptScreen, PresentationSource presentationSource) { // For now we only know how to use HwndSource. HwndSource inputSource = presentationSource as HwndSource; if (inputSource == null) { return(ptScreen); } HandleRef handleRef; new UIPermission(UIPermissionWindow.AllWindows).Assert(); // BlessedAssert: try { handleRef = new HandleRef(inputSource, inputSource.Handle); } finally { UIPermission.RevertAssert(); } // Convert the point from screen coordinates back to client coordinates. NativeMethods.POINT ptClient = new NativeMethods.POINT((int)ptScreen.X, (int)ptScreen.Y); SafeNativeMethods.ScreenToClient(handleRef, ptClient); // MITIGATION: WIN32_AND_AVALON_RTL // // When a window is marked with the WS_EX_LAYOUTRTL style, Win32 // mirrors the coordinates during the various translation APIs. // // Avalon also sets up mirroring transforms so that we properly // mirror the output since we render to DirectX, not a GDI DC. // // Unfortunately, this means that our coordinates are already mirrored // by Win32, and Avalon mirrors them again. To work around this // problem, we un-mirror the coordinates from Win32 before hit-testing // in Avalon. // int windowStyle = SafeNativeMethods.GetWindowStyle(handleRef, true); if ((windowStyle & NativeMethods.WS_EX_LAYOUTRTL) == NativeMethods.WS_EX_LAYOUTRTL) { NativeMethods.RECT rcClient = new NativeMethods.RECT(); SafeNativeMethods.GetClientRect(handleRef, ref rcClient); ptClient.x = rcClient.right - ptClient.x; } return(new Point(ptClient.x, ptClient.y)); }
/// <summary> /// Convert a point from the coordinate space of the screen into /// the "client" coordinate space of a window. /// </summary> internal static Point ScreenToClient(Point pointScreen, PresentationSource presentationSource) { // For now we only know how to use HwndSource. HwndSource inputSource = presentationSource as HwndSource; if (inputSource == null) { return(pointScreen); } HandleRef handleRef = new HandleRef(inputSource, inputSource.CriticalHandle); NativeMethods.POINT ptClient = FromPoint(pointScreen); SafeNativeMethods.ScreenToClient(handleRef, ptClient); ptClient = AdjustForRightToLeft(ptClient, handleRef); return(ToPoint(ptClient)); }