CDC* pDC = GetDC(); CString str = "Hello, World!"; CSize size = pDC->GetTextExtent(str); ReleaseDC(pDC); //This code gets a device context, sets a string, gets the text extent of the string, and then releases the device context.
CFont font; CSize size; font.CreateFont(24, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial"); CFont* pOldFont = pDC->SelectObject(&font); size = pDC->GetTextExtent("Hello, World!", 13); pDC->SelectObject(pOldFont); font.DeleteObject(); //This code creates a font, selects the font into the device context, gets the text extent of a string, deselects the font, and then deletes it.