tags:

views:

220

answers:

2

This is an easy way to draw some text with a default font.

pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->DrawText(text, -1, rc, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);

How can I do exactly the same, but in bold type... same font but bold? Can it be done without creating custom CFont object?

A: 

Get the font data for the stock font into a LOGFONT struct (GetObject() in plain GDI). Change the weight parameter to bold. Use CreateFontIndirect() to make a font from that LOGFONT struct. Select the font into the device context.

Then draw the text as usual.

Seva Alekseyev
A: 
CFont* pOldFont = pDC->GetCurrentFont();

LOGFONT logFont;
pOldFont->GetLogFont(&logFont);
logFont.lfWeight = FW_BOLD;

CFont newFont;
newFont.CreateFontIndirect(&logFont);

pDC->SelectObject(&newFont);
pDC->DrawText();
pDC->SelectObject(pOldFont);
Nikola Smiljanić
newFont.CreateFontIndirect(
John
Seva Alekseyev
Works beautifully, thanks!
John