int nFont = 1; int nWidth = 60; int nHeight = 60; nFont = 1 + strText.length() / 6; SIZE szText = { 0 }; HFONT hFont = m_PaintManager.GetFont(nFont); HFONT hOldFont = (HFONT)SelectObject(m_PaintManager.GetPaintDC(), (HGDIOBJ)hFont); if (! GetTextExtentPoint32(m_PaintManager.GetPaintDC(), strText.c_str(), strText.length(), &szText)) { ERROR("%s: GetTextExtentPoint32 Fail Error=%u\n", __FUNCTION__, GetLastError()); szText.cx = strText.length() * 10; szText.cy = 30; } else { INFO("%s: text=%S, szText=[%d,%d]\n", __FUNCTION__, strText.c_str(), szText.cx, szText.cy); } SelectObject(m_PaintManager.GetPaintDC(), (HGDIOBJ)hOldFont);Copy the code
1. M_paintmanager. GetFont sets the size of the string
2. Execute SelectObject to replace HFONT
GetTextExtentPoint32 gets the character size, mainly on szText
4. Brush SelectObject again
Shenzhen program exchange group 550846167