diff options
-rw-r--r-- | win32/PlatWin.cxx | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index 99885e5e4..60bdbfee6 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -251,6 +251,10 @@ UINT uSystemDPI = USER_DEFAULT_SCREEN_DPI; using GetDpiForWindowSig = UINT(WINAPI *)(HWND hwnd); GetDpiForWindowSig fnGetDpiForWindow = nullptr; +HMODULE hDLLShcore {}; +using GetDpiForMonitorSig = HRESULT (WINAPI *)(HMONITOR hmonitor, /*MONITOR_DPI_TYPE*/int dpiType, UINT *dpiX, UINT *dpiY); +GetDpiForMonitorSig fnGetDpiForMonitor = nullptr; + using GetSystemMetricsForDpiSig = int(WINAPI *)(int nIndex, UINT dpi); GetSystemMetricsForDpiSig fnGetSystemMetricsForDpi = nullptr; @@ -272,6 +276,13 @@ void LoadDpiForWindow() noexcept { uSystemDPI = ::GetDeviceCaps(hdcMeasure, LOGPIXELSY); ::DeleteDC(hdcMeasure); } + + if (!fnGetDpiForWindow) { + hDLLShcore = ::LoadLibraryExW(L"shcore.dll", {}, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (hDLLShcore) { + fnGetDpiForMonitor = DLLFunction<GetDpiForMonitorSig>(hDLLShcore, "GetDpiForMonitor"); + } + } } HINSTANCE hinstPlatformRes {}; @@ -448,6 +459,14 @@ UINT DpiForWindow(WindowID wid) noexcept { if (fnGetDpiForWindow) { return fnGetDpiForWindow(HwndFromWindowID(wid)); } + if (fnGetDpiForMonitor) { + HMONITOR hMonitor = ::MonitorFromWindow(HwndFromWindowID(wid), MONITOR_DEFAULTTONEAREST); + UINT dpiX = 0; + UINT dpiY = 0; + if (fnGetDpiForMonitor(hMonitor, 0 /*MDT_EFFECTIVE_DPI*/, &dpiX, &dpiY) == S_OK) { + return dpiY; + } + } return uSystemDPI; } @@ -3413,6 +3432,10 @@ void Platform_Finalise(bool fromDllMain) { } } #endif + if (!fromDllMain && hDLLShcore) { + FreeLibrary(hDLLShcore); + hDLLShcore = {}; + } ListBoxX_Unregister(); } |