From 2b19fd174f9405adcf6d48685ff237767c5d9c4c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 11 May 2023 18:42:02 +0100 Subject: [PATCH] Fix wxMSW wxWindow::GetTextExtent() with given font in high DPI This function returned wrong results when using a non-default font in non-standard DPI because it didn't adjust the font to use the window DPI, as needs to be done manually in wxMSW before using it. Fix this now and do the same thing here as we already did in wxWindow::SetFont() (via WXAdjustFontToOwnPPI()) or in wxDC::SetFont(). See #23542. (cherry picked from commit f3a4767455f62d1a614eac6d538599ac738d3918) --- docs/changes.txt | 1 + src/msw/textmeasure.cpp | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index c9552ad262..c77df62fe7 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -283,6 +283,7 @@ wxMSW: - Fix wxTreeCtrl::ScrollTo() with hidden root item (#23534). - Fix hang in wxFileDialog if COINIT_MULTITHREADED was used (#23578). - Fix handling of RLE-compressed bitmaps (Brian Nixon, #23573). +- Fix wxWindow::GetTextExtent() with given font in high DPI (#23542). wxOSX: diff --git a/src/msw/textmeasure.cpp b/src/msw/textmeasure.cpp index 69bbfc2457..97b1e58822 100644 --- a/src/msw/textmeasure.cpp +++ b/src/msw/textmeasure.cpp @@ -69,8 +69,25 @@ void wxTextMeasure::BeginMeasuring() // also if we're associated with a window because the window HDC created // above has the default font selected into it and not the font of the // window. - if ( m_font || m_win ) - m_hfontOld = (HFONT)::SelectObject(m_hdc, GetHfontOf(GetFont())); + wxFont font; + if ( m_font ) + { + font = *m_font; + + // We also need to adjust this font to the DPI used by the window if + // both are given. + if ( m_win ) + font.WXAdjustToPPI(m_win->GetDPI()); + } + else if ( m_win ) + { + // This font doesn't need DPI adjustment. + font = m_win->GetFont(); + } + //else: no need to do anything when using wxDC with its default font. + + if ( font.IsOk() ) + m_hfontOld = (HFONT)::SelectObject(m_hdc, GetHfontOf(font)); } void wxTextMeasure::EndMeasuring()