Draw Text Directly to the Screen 

The example below details how to draw text directly to a window using the GDI+ Graphics DrawString method. The various parameters required to describe the appearance of the text on-screen are generated first.

// Create the font family
CComBSTR bstrFontName(m_strFont.c_str());
FontFamily fontFamily(bstrFontName);

// Create the font
RECT rctDrawArea = {0, 0, m_lPageWidth, m_lPageHeight};
REAL rFontSize = this->CalculateFontSize(rctDrawArea, m_lSize);
FontStyle fsStyle = this->GetFontStyle(m_strStyle);
Font font(&fontFamily, rFontSize, fsStyle, UnitPoint);

// Select the drawing colour
Color color(m_ucAlpha, m_ucRed, m_ucGreen, m_ucBlue);
SolidBrush solidBrush(color);

// Set the text alignment
StringFormat format;
StringAlignment Alignment = this->GetStringAlignment(m_strAlign);
format.SetAlignment(Alignment);

// Set the position of the text
this->CalculateTextPosition(rctDrawArea, m_lXPos, m_lYPos);
PointF orgPosition(m_lXPos, m_lYPos);

// Determine whether any scaling is required
if(m_pScaleMatrix == NULL) m_pScaleMatrix = new Matrix();
this->ScaleText(hWnd, &m_pScaleMatrix);
pCanvas->SetTransform(m_pScaleMatrix);

// Set the text rendering mode
pCanvas->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);

// Retain the text bounding rectangle coordinates for later
CComBSTR bstrText(m_strText.c_str());
pCanvas->MeasureString(bstrText, (INT)m_strText.length(), &font, orgPosition, &format, &m_rctTextBounds);

// Draw the text
pCanvas->DrawString(bstrText, (INT)m_strText.length(), &font, orgPosition, &format, &solidBrush);

// Reset the world transformation matrix
pCanvas->ResetTransform();

// Clean up resources
bstrText.Empty();
bstrFontName.Empty();