|
-
January 7th, 2023, 02:34 AM
#11
Re: Help with mfc
You've probably got the "System" font, which size can be modified only from the system settings. Or can be another font that does not support the size you have provided.
Anyway it's not a good idea to get the current font from the device context and modify its attributes "on the fly".
Just create your own font! Here is an example:
Code:
void CDemoSdiView::RenderText(CDC* pDC, int left, int top, const CString& strText)
{
CFont font;
font.CreatePointFont(80, _T("Verdana"));
CFont* pOldFont = pDC->SelectObject(&font);
int nOldMapMode = pDC->SetMapMode(MM_TEXT);
CRect rc;
// calculate the necessary rectangle
pDC->DrawText(strText, rc, DT_LEFT|DT_TOP|DT_CALCRECT);
// offset the rectangle to top-left coordinates
rc.OffsetRect(left, top);
// filally draw the text
pDC->DrawText(strText, rc, DT_LEFT|DT_TOP);
// restore DC status
pDC->SetMapMode(nOldMapMode);
pDC->SelectObject(pOldFont);
}
Additional notes:
- After drawing, do not forget to restore the initial DC status, as shown in the above example. Alternatively, you can use CDC::SaveDC and CDC::RestoreDC for doing that easier.
Code:
// save the DC status
int nDCStatus = pDC->SaveDC();
// ...
// ...
// restore DC status
pDC->RestoreDC(nDCStatus);
- For simplicity, I used in my example CFont::CreatePointFont. If you want to also set other custom attributes, like weight for example, call CFont::CreateFont, CFont::CreateFontIndirect, or CFont::CreatePointFontIndirect. See CFont Class in Microsoft Learn documentation.
- The fizical size of a text depends on DC type and mapping mode. That's the reason I called CDC::SetMapMode. For more info, have a look at LOGFONT Structure documentation.
Last edited by ovidiucucu; January 7th, 2023 at 02:40 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|