aboutsummaryrefslogtreecommitdiffhomepage
path: root/win32/PlatWin.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'win32/PlatWin.cxx')
-rw-r--r--win32/PlatWin.cxx23
1 files changed, 23 insertions, 0 deletions
diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx
index bb808b4e7..c97b9283b 100644
--- a/win32/PlatWin.cxx
+++ b/win32/PlatWin.cxx
@@ -157,11 +157,15 @@ GetSystemMetricsForDpiSig fnGetSystemMetricsForDpi = nullptr;
using AdjustWindowRectExForDpiSig = BOOL(WINAPI *)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
AdjustWindowRectExForDpiSig fnAdjustWindowRectExForDpi = nullptr;
+using SetThreadDpiAwarenessContextSig = DPI_AWARENESS_CONTEXT(WINAPI *)(DPI_AWARENESS_CONTEXT);
+SetThreadDpiAwarenessContextSig fnSetThreadDpiAwarenessContext = nullptr;
+
void LoadDpiForWindow() noexcept {
HMODULE user32 = ::GetModuleHandleW(L"user32.dll");
fnGetDpiForWindow = DLLFunction<GetDpiForWindowSig>(user32, "GetDpiForWindow");
fnGetSystemMetricsForDpi = DLLFunction<GetSystemMetricsForDpiSig>(user32, "GetSystemMetricsForDpi");
fnAdjustWindowRectExForDpi = DLLFunction<AdjustWindowRectExForDpiSig>(user32, "AdjustWindowRectExForDpi");
+ fnSetThreadDpiAwarenessContext = DLLFunction<SetThreadDpiAwarenessContextSig>(user32, "SetThreadDpiAwarenessContext");
using GetDpiForSystemSig = UINT(WINAPI *)(void);
GetDpiForSystemSig fnGetDpiForSystem = DLLFunction<GetDpiForSystemSig>(user32, "GetDpiForSystem");
@@ -358,6 +362,25 @@ struct FontDirectWrite : public FontWin {
}
+HMONITOR MonitorFromWindow(HWND hWnd) noexcept {
+ constexpr DWORD monitorFlags = MONITOR_DEFAULTTONEAREST;
+
+ if (!fnSetThreadDpiAwarenessContext) {
+ return ::MonitorFromWindow(hWnd, monitorFlags);
+ }
+
+ // Temporarily switching to PerMonitorV2 to retrieve correct monitor via MonitorFromRect() in case of active GDI scaling.
+ const DPI_AWARENESS_CONTEXT oldContext = fnSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
+ PLATFORM_ASSERT(oldContext != nullptr);
+
+ RECT rect;
+ ::GetWindowRect(hWnd, &rect);
+ const HMONITOR monitor = ::MonitorFromRect(&rect, monitorFlags);
+
+ fnSetThreadDpiAwarenessContext(oldContext);
+ return monitor;
+}
+
std::shared_ptr<Font> Font::Allocate(const FontParameters &fp) {
#if defined(USE_D2D)
if (fp.technology != Technology::Default) {