From 04b99d54bd9841a331c4656a411ca17960be38f3 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 22 Jul 2018 14:03:42 +0200 Subject: [PATCH] Add DPI Aware replacement of GetSystemMetrics Use GetSystemMetricsForDpi when this function is available and the DPI is known. Otherwise keep using GetSystemMetrics. --- include/wx/msw/private.h | 1 + src/msw/window.cpp | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 1a426fd9ba..575c598558 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -300,6 +300,7 @@ extern HICON wxBitmapToHICON(const wxBitmap& bmp); extern HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY); +extern int wxGetSystemMetrics(int nIndex, const wxWindow* win); #if wxUSE_OWNER_DRAWN diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 58d88811d2..021e161b70 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -4701,6 +4701,66 @@ wxWindowMSW::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct) return false; } +// --------------------------------------------------------------------------- +// DPI +// --------------------------------------------------------------------------- + +namespace +{ + +static inline const wxTopLevelWindow* wxGetWinTLW(const wxWindow* win) +{ + if ( win ) + { + const wxWindow* tlwWin = wxGetTopLevelParent(const_cast(win)); + return wxDynamicCast(tlwWin, wxTopLevelWindow); + } + else if ( wxTheApp ) + { + wxWindow* window = wxTheApp->GetTopWindow(); + if ( window ) + { + return wxDynamicCast(wxGetTopLevelParent(window), wxTopLevelWindow); + } + } + + return NULL; +} + +} + +/*extern*/ +int wxGetSystemMetrics(int nIndex, const wxWindow* win) +{ +#if wxUSE_DYNLIB_CLASS + const wxTopLevelWindow* tlw = wxGetWinTLW(win); + + if ( tlw ) + { + typedef int (WINAPI * GetSystemMetricsForDpi_t)(int nIndex, UINT dpi); + static GetSystemMetricsForDpi_t s_pfnGetSystemMetricsForDpi = NULL; + static bool s_initDone = false; + + if ( !s_initDone ) + { + wxLoadedDLL dllUser32("user32.dll"); + wxDL_INIT_FUNC(s_pfn, GetSystemMetricsForDpi, dllUser32); + s_initDone = true; + } + + if ( s_pfnGetSystemMetricsForDpi ) + { + const int y = ::GetDeviceCaps(::GetDC(tlw->GetHWND()), LOGPIXELSY); + return s_pfnGetSystemMetricsForDpi(nIndex, (UINT)y); + } + } +#else + wxUnusedVar(win); +#endif // wxUSE_DYNLIB_CLASS + + return ::GetSystemMetrics(nIndex); +} + // --------------------------------------------------------------------------- // colours and palettes // ---------------------------------------------------------------------------