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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 217
Separate into files by filename 12 87
sum67 challenge 35 107
Annoying "thing" blocks my view 4 113
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

789 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