From: Vadim Zeitlin Date: Wed, 30 Jun 2004 15:47:01 +0000 (+0000) Subject: made GetTextExtent() work correctly with NULL theFont parameter (m_font might nto... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/64869ab787854e69b35893728fadd06ec69165e9?ds=inline made GetTextExtent() work correctly with NULL theFont parameter (m_font might nto be set); drastically simplified it by using helper WindowHDC and SelectInHDC classes instead of the old mess git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28105 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 0bfc228932..ce75422271 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -1663,31 +1663,18 @@ void wxWindowMSW::GetTextExtent(const wxString& string, int *descent, int *externalLeading, const wxFont *theFont) const { - const wxFont *fontToUse = theFont; - if ( !fontToUse ) - fontToUse = &m_font; + wxASSERT_MSG( !theFont || theFont->Ok(), + _T("invalid font in GetTextExtent()") ); - HWND hWnd = GetHwnd(); - HDC dc = ::GetDC(hWnd); + const wxFont fontToUse(theFont ? *theFont : GetFont()); - HFONT fnt = 0; - HFONT hfontOld = 0; - if ( fontToUse && fontToUse->Ok() ) - { - fnt = (HFONT)((wxFont *)fontToUse)->GetResourceHandle(); // const_cast - if ( fnt ) - hfontOld = (HFONT)SelectObject(dc,fnt); - } + WindowHDC hdc(GetHwnd()); + SelectInHDC selectFont(hdc, GetHfontOf(fontToUse)); SIZE sizeRect; TEXTMETRIC tm; - GetTextExtentPoint(dc, string, (int)string.Length(), &sizeRect); - GetTextMetrics(dc, &tm); - - if ( fontToUse && fnt && hfontOld ) - SelectObject(dc, hfontOld); - - ReleaseDC(hWnd, dc); + GetTextExtentPoint(hdc, string, string.length(), &sizeRect); + GetTextMetrics(hdc, &tm); if ( x ) *x = sizeRect.cx;