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
This commit is contained in:
Vadim Zeitlin
2004-06-30 15:47:01 +00:00
parent 918107037d
commit 64869ab787

View File

@@ -1663,31 +1663,18 @@ void wxWindowMSW::GetTextExtent(const wxString& string,
int *descent, int *externalLeading, int *descent, int *externalLeading,
const wxFont *theFont) const const wxFont *theFont) const
{ {
const wxFont *fontToUse = theFont; wxASSERT_MSG( !theFont || theFont->Ok(),
if ( !fontToUse ) _T("invalid font in GetTextExtent()") );
fontToUse = &m_font;
HWND hWnd = GetHwnd(); const wxFont fontToUse(theFont ? *theFont : GetFont());
HDC dc = ::GetDC(hWnd);
HFONT fnt = 0; WindowHDC hdc(GetHwnd());
HFONT hfontOld = 0; SelectInHDC selectFont(hdc, GetHfontOf(fontToUse));
if ( fontToUse && fontToUse->Ok() )
{
fnt = (HFONT)((wxFont *)fontToUse)->GetResourceHandle(); // const_cast
if ( fnt )
hfontOld = (HFONT)SelectObject(dc,fnt);
}
SIZE sizeRect; SIZE sizeRect;
TEXTMETRIC tm; TEXTMETRIC tm;
GetTextExtentPoint(dc, string, (int)string.Length(), &sizeRect); GetTextExtentPoint(hdc, string, string.length(), &sizeRect);
GetTextMetrics(dc, &tm); GetTextMetrics(hdc, &tm);
if ( fontToUse && fnt && hfontOld )
SelectObject(dc, hfontOld);
ReleaseDC(hWnd, dc);
if ( x ) if ( x )
*x = sizeRect.cx; *x = sizeRect.cx;