From 4a76872f544dcb3dbac8c15c62e76a7158a7fe90 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 13 Aug 2024 12:09:01 +0100 Subject: [PATCH] Windows: Add helper to find client rect in screen coordinates --- .../native/juce_Windowing_windows.cpp | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp index 7786de2358..eea781608d 100644 --- a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp +++ b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp @@ -1680,13 +1680,7 @@ public: return snapped; } - RECT rect{}; - GetClientRect (hwnd, &rect); - auto points = readUnaligned> (&rect); - MapWindowPoints (hwnd, nullptr, points.data(), (UINT) points.size()); - - const auto mappedRect = readUnaligned (points.data()); - const auto logicalClient = convertPhysicalScreenRectangleToLogical (D2DUtilities::toRectangle (mappedRect), hwnd); + const auto logicalClient = convertPhysicalScreenRectangleToLogical (getClientRectInScreen(), hwnd); return logicalClient; } @@ -1798,6 +1792,19 @@ public: return wp.showCmd == SW_SHOWMAXIMIZED; } + Rectangle getClientRectInScreen() const + { + ScopedThreadDPIAwarenessSetter setter { hwnd }; + + RECT rect{}; + GetClientRect (hwnd, &rect); + auto points = readUnaligned> (&rect); + MapWindowPoints (hwnd, nullptr, points.data(), (UINT) points.size()); + const auto result = readUnaligned (&points); + + return D2DUtilities::toRectangle (result); + } + bool contains (Point localPos, bool trueIfInAChildWindow) const override { auto r = convertPhysicalScreenRectangleToLogical (D2DUtilities::toRectangle (getWindowScreenRect (hwnd)), hwnd); @@ -1808,17 +1815,7 @@ public: const auto screenPos = convertLogicalScreenPointToPhysical (localPos + getScreenPosition(), hwnd); if (trueIfInAChildWindow) - { - // Quick check to see whether the point is inside the client bounds - RECT rect; - GetClientRect (hwnd, &rect); - POINT points[2]; - memcpy (points, &rect, sizeof (points)); - MapWindowPoints (hwnd, nullptr, points, (UINT) std::size (points)); - memcpy (&rect, points, sizeof (points)); - - return PtInRect (&rect, D2DUtilities::toPOINT (screenPos)); - } + return getClientRectInScreen().contains (screenPos); auto w = WindowFromPoint (D2DUtilities::toPOINT (screenPos));