I would like to draw text into a GDI device context DC using the same font but with different styles. I create a CFont
object, select that font object into the DC and then I use DC.DrawText
to draw text into that DC using that font.
The context is that i want to draw a diagram with axis labels and axis titles using the same font but with different properties like normal or bold and different orientations.
However to stay minimalistic, let's say i want to draw text horizontally in normal and bold as well as vertically in normal and bold. This would make up 4 different styles.
I came up with essentially a code like this:
int size_in_pt = 12;
const TCHAR *fontname = _T("Tahoma");
CFont myfont;
CClientDC dc(this);
CString textToPrint(_T("text1234"));
CRect textRect;
myFont.CreateFont(size_in_pt, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, fontname);
dc.SelectObject(&myFont);
dc.DrawText(textToPrint, textRect, DT_LEFT);
textRect.MoveToX(100);
myFont.CreateFont(size_in_pt, 0, 0, -90, 0, 0, 0, 0, 0, 0, 0, 0, 0, fontname);
dc.SelectObject(&myFont);
dc.DrawText(textToPrint, textRect, DT_LEFT);
textRect.MoveToY(100);
myFont.CreateFont(size_in_pt, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, fontname);
dc.SelectObject(&myFont);
dc.DrawText(textToPrint, textRect, DT_LEFT);
textRect.MoveToX(0);
myFont.CreateFont(size_in_pt, 0, 0, -90, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, fontname);
dc.SelectObject(&myFont);
dc.DrawText(textToPrint, textRect, DT_LEFT);
You can see that it is quite annoying that i have to CreateFont
and SelectObject
several times.
I don't like this. I would like to create the font one time and select it into the DC one time and then only change the font properties like bold and orientation between each DrawText
.
Do you have experience with such a case and maybe have an idea how to make to code more elegant and less repetitive?