CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    MFC General: How do I change the font of a control?

    Q: How do I change the font of a control?

    A: The following is an example how to change the font for a static control...the example is a dialog-based application which does not matter regarding the setting of the font itself...

    Code:
    // Dialog.hpp
    class CYourDlg : public CDialog
    {
    public:
      ~CYourDlg() { m_Font.DeleteObject(); }
      ...
    
    private:
      CFont m_Font;
    };
    
    // Dialog.cpp
    BOOL CYourDlg::OnInitDialog()
    {
      CDialog::OnInitDialog();
    
      // Creates a 12-point-Courier-font
      m_Font.CreatePointFont(120, _T("Courier"));
    
      // With a member variable associated to the static control
      m_MyStatic.SetFont(&m_Font);
    
      // Without a member variable
      GetDlgItem(IDC_MY_STATIC)->SetFont(&m_Font);
    }
    The font itself can be created in many ways...the only important thing is that the font object itself exists as long as the control...in other words....keep it as a member of the control or surrounding dialog....

    Code:
    // First way
    CFont Font;
    
    Font.CreateFont(12,                            // Height
                    0,                             // Width
                    0,                             // Escapement
                    0,                             // Orientation
                    FW_BOLD,                       // Weight
                    FALSE,                         // Italic
                    TRUE,                          // Underline
                    0,                             // StrikeOut
                    ANSI_CHARSET,                  // CharSet
                    OUT_DEFAULT_PRECIS,            // OutPrecision
                    CLIP_DEFAULT_PRECIS,           // ClipPrecision
                    DEFAULT_QUALITY,               // Quality
                    DEFAULT_PITCH | FF_SWISS,      // PitchAndFamily
                    "Arial"));                     // Facename
    
    
    
    // Second way
    CFont   Font;
    LOGFONT lfLogFont;
    
    memset(&lfLogFont, 0, sizeof(lfLogFont));
    
    lfLogFont.lfHeight    = 12;                    // 12-pixel-height
    lfLogFont.lfWeight    = FW_BOLD;               // Bold
    lfLogFont.lfUnderline = TRUE;                  // Underlined
    
    strcpy(lfLogFont.lfFaceName, "Arial");         // Arial
    
    Font.CreateFontIndirect(&lfLogFont);
    
    
    
    // Third way
    CFont   Font;
    LOGFONT lfLogFont;
    
    memset(&lfLogFont, 0, sizeof(lfLogFont));
    
    lfLogFont.lfHeight    = 120;                   // 12-pixel-height
    lfLogFont.lfWeight    = FW_BOLD;               // Bold
    lfLogFont.lfUnderline = TRUE;                  // Underlined
    
    strcpy(lfLogFont.lfFaceName, "Arial");         // Arial
    
    Font.CreatePointFontIndirect(&lfLogFont);

    Last edited by Andreas Masur; July 24th, 2005 at 03:49 PM.

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