From 3bb3534f5f4fcfe25e2fa2eb7ad47ffb9540f656 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Fri, 26 Jan 2024 11:14:29 -0800 Subject: [PATCH] Fix reporting the location of a Hwnd form with multi-monitor DPIs --- src/Eto.WinForms/Forms/HwndFormHandler.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Eto.WinForms/Forms/HwndFormHandler.cs b/src/Eto.WinForms/Forms/HwndFormHandler.cs index 18b57036f..62f91f7a2 100755 --- a/src/Eto.WinForms/Forms/HwndFormHandler.cs +++ b/src/Eto.WinForms/Forms/HwndFormHandler.cs @@ -81,25 +81,26 @@ public float LogicalPixelSize { if (Win32.PerMonitorDpiSupported) return Win32.GetWindowDpi(Control) / 96.0f; - var screen = swf.Screen.FromHandle(Control); - if (screen == null) - return 1; - return screen.GetLogicalPixelSize(); + return SwfScreen?.GetLogicalPixelSize() ?? 1; } } + swf.Screen SwfScreen => swf.Screen.FromHandle(Control); + public Eto.Drawing.Point Location { get { - Win32.RECT rect; - Win32.GetWindowRect(Control, out rect); - var location = new PointF(rect.left, rect.top); - return Point.Round(location / (float)LogicalPixelSize); + var rect = new Win32.RECT(); + Win32.ExecuteInDpiAwarenessContext(() => Win32.GetWindowRect(Control, out rect)); + + var location = new Point(rect.left, rect.top); + return Point.Round(location.ScreenToLogical(SwfScreen)); } set { - throw new NotImplementedException(); + var loc = ((PointF)value).LogicalToScreen(); + Win32.ExecuteInDpiAwarenessContext(() => Win32.SetWindowPos(Control, IntPtr.Zero, loc.X, loc.Y, 0, 0, Win32.SWP.NOSIZE | Win32.SWP.NOACTIVATE)); } }