Easy from a beginner

Posted on 1998-10-29
Last Modified: 2013-11-20
I'm new to Vc and MFC, this is a simple question that i don't know how to solve it.

How can i change the font, and colors of a static, and text edit control?
Question by:PedroMVGomes
1 Comment

Accepted Solution

Answers2000 earned 50 total points
ID: 1324012
Sorry this is a bit disjointed (cut and pasting from my previous answers and my notes)


   You can do this in the dialog, or derive a class from CEdit (and override the reflection
   version which has an = sign before the name).

   When you override this message you
   1. Can set the text color using
   pDC->SetTextColor( RGB(whatever) ) ;

   2. Can set the background using
   pDC->SetBkColor( RGB(whatever) ) ;
   and returning an HBRUSH wish a solid color

   Therefore the steps are
   A. Add an HBRUSH to the dialog class as a member variable
   B. In your constructor create a brush or brushes

m_hBrush = ::CreateSolidBrush( RGB(whatever) ) ;

//     example

//     m_hBrushWhite = ::CreateSolidBrush( RGB(255,255,255) ) ;

   C. In your destructor destroy the brush or brushes

   DeleteObject( m_hBrush ) ;

   D. Override the WM_CTLCOLOR message.  You can check it's the control you're
   interesting in by checking the pWnd and nCtlColor values

   HBRUSH CMyDialog::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
      if ( _red_ )
        pDC->SetTextColor( RGB(255,0,0) ) ;
        pDC->SetTextColor( RGB(255,255,255) ) ;
        return m_hBrush ;

      // etc

   E. When you want to force a repaint of the particular control set a flag indicating if it's red,
   green or whatever, then force a repaint as

   GetDlgItem(ID_MYEDIT)->InvalidateRect( NULL, TRUE ) ;
   GetDlgItem(ID_MYEDIT)->UpdateWindow() ;

   F. So you know RGB values you  might find useful

   RGB(255,0,0) =  bright red
   RGB(0,0,255) = bright green
   RGB(255,255,255) = white
   RGB(0,0,0) = black

   RGB(128,0,0) = dark red
   RGB(0,0,128) = dark green

Procedure is the same for static & edit controls


         Send a WM_SETFONT message to the control to set the font.

         You will need an HFONT handle to a font.  You can get one use GetStockObject or by creating a font using CreateFont/CreateFontIndirect

         The WM_SETFONT message includes 2 params :
         (i) WPARAM = the HFONT of the new font
         (ii) LPARAM = contains a flag indicating if control should redraw on immediately as a result of the font change.

         Say m_edit is your CEdit associated with the control

         // set to fixed pitch font
         m_edit.SendMessage( WM_SETFONT, (WPARAM)(HFONT)::GetStockObject(ANSI_FIXED_FONT), MAKELPARAM(TRUE,0) ) ;

Here's how to do custom fonts  :-

         (a) Add HFONT member varaible to the dialog, say m_hFont ;
         (b) Look up LOGFONT structure in the help.  Declare one and fill in your constructor for the dialog class, then create font using this
         LOGFONT lf ;
         lf.lfHeight = ...etc.
         m_hFont = ::CreateFontIndirect(&lf) ;
         (c) In OnInitDialog use WM_SETFONT message to set this font as I showed in my prev comment
         (d) In your destructor delete the font

The procedure for statics & edits is again the same

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 93
Excel Use VBA to get user's Mac Address for their computer 5 181
How to convert MFC::CString to UTF8 wchar_t* 10 215
wordmultiple challenge 12 136
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question