CDC* pDC = GetDC(); CString str = _T("Hello, world!"); pDC->DrawText(str, CRect(10, 10, 100, 100), DT_LEFT | DT_TOP); ReleaseDC(pDC);
CDC* pDC = GetDC(); CString str = _T("Hello, world!"); CRect rect = {0, 0, 0, 0}; pDC->DrawText(str, &rect, DT_CALCRECT); pDC->DrawText(str, rect, DT_CENTER | DT_VCENTER); ReleaseDC(pDC);
CDC* pDC = GetDC(); CString str = _T("Hello, world!"); LOGFONT lf; lf.lfHeight = -MulDiv(12, GetDeviceCaps(pDC->m_hDC, LOGPIXELSY), 72); lf.lfWeight = FW_NORMAL; strcpy_s(lf.lfFaceName, _T("Arial")); CFont font; font.CreateFontIndirect(&lf); CFont* oldFont = pDC->SelectObject(&font); pDC->DrawText(str, CRect(10, 10, 100, 100), DT_LEFT | DT_TOP); pDC->SelectObject(oldFont); font.DeleteObject(); ReleaseDC(pDC);This example sets the font to Arial with a size of 12 points and a normal weight, and then selects the font into the device context using the SelectObject method. The font is drawn using the DrawText method, with the same rectangle and text alignment as in Example 1. Finally, the original font is restored and then deleted to prevent a memory leak. In all of these examples, the CDC object is part of the Microsoft Foundation Class (MFC) library.