CFont* pFont = new CFont(); pFont->CreateFont(18, // font height 9, // average character width 0, // escapement 0, // orientation FW_NORMAL, // weight FALSE, // italic FALSE, // underline 0, // strikeout ANSI_CHARSET, // character set OUT_DEFAULT_PRECIS,// output precision CLIP_DEFAULT_PRECIS,// clip precision DEFAULT_QUALITY, // output quality DEFAULT_PITCH, // pitch and family _T("Arial")); // font name CFont* pOldFont = pDC->SelectObject(pFont); pDC->TextOut(50, 50, _T("Hello, world!")); pDC->SelectObject(pOldFont); delete pFont;
CRect rect(50, 50, 200, 150); // Create and select a new brush object CBrush brush(RGB(192, 192, 192)); CBrush* pOldBrush = pDC->SelectObject(&brush); // Draw the shaded rectangle pDC->FillRect(&rect, &brush); // Select the old brush back into the device context pDC->SelectObject(pOldBrush);This code creates a rectangle object with the given dimensions, creates a new brush object with a gray color, selects the brush object into the device context, fills the rectangle with the brush, and then selects the old brush object back into the device context. The CDC DrawState feature is a part of the Microsoft Foundation Class (MFC) library for C++ programming.