CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 29

Thread: Help with mfc

Threaded View

  1. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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
  •  





Click Here to Expand Forum to Full Width

Featured