Changing Font/appearance of MFC controls

How do I change the font, background color etc for standard controls like static text, button. ?

I know that I should derive a class from say CStatic ( for static text ) or CButton ( for button).

Which handler functions should I override to change the properties of the device context and to customize the painting of the control ?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Answers2000Connect With a Mentor Commented:
1. Use the WM_SETFONT message to change the font of control (e.g. button or static).  Basically you need an HFONT (you can get one from a CFont or use the API functions - GetStockObject _or_ ::CreateFont _or_ ::CreateFontIndirect_) to get one.

Then send it to the control window

If you are doing it from the control itself, you can do this at any point after the control has been created (or attached to an existing window - remember OnCreate is not called for control windows which are on dialogs, the Windows dialog manager creates the Windows, then the control windows are attached)

Each CStatic, CButton (and all other classes derived from CWnd) include an HWND variable called m_hWnd.  This is the window to set the font change to.

Before your control is destroyed (e.g. OnDestroy), set the font back (you can get the previous font by using the WM_GETFONT message), and destroy any fonts you have created.

You can find SDK style code at this URL (but this shows you how to use WM_SETFONT and WM_GETFONT)

It is not necessary to derive a control class.  You can use these messages even for the standard MFC classes.

2. To change color of a control, handle the WM_CTLCOLOR message

If you are using a derived class of CButton or CStatic etc.. use the version with = inside.

Set the foreground color by doing

pDC->SetTextColor( RGB(some_value_) ) ;

Set the background color by doing

pDC->SetBkColor( RGB(some_value) ) ;
and return an HBRUSH handle to the background brush,

i. add to class an HBRUSH member
ii. Create a solid brush color in your constructor using CreateSolidBrush
iii. In destructor use DeleteObject on the brush.

If you want to change the color on the fly, recreate the brush (delete any old one first, store the colors in class member variables, and then force a repaint using Invalidate() followed by UpdateWindow() ).

Again it is not necessary to derive from CStatic/CButton etc. to do this, but it is often easier.  The other way is to allow the containing window (e.g. a dialog box with the controls to handle the WM_CTLCOLOR message),  when you do it this way, you should remember the dialog may have many controls each requesting what color they should be - however this version of OnCtlColor gives you extra parameters telling you which window is about to be painted (so you can supply different colors for different Windows).

3. There is another way to do it, which is to paint the control yourself.  You can do this for any control by handling WM_PAINT messages yourself (you must derive to do this).  For buttons you can set the owner draw style, and handle the WM_DRAWITEM message.  In the case of simply changing the color/font this is overkill and requires more code than you would want to write in this case.  The advantage with this approach, is that you can paint anything you like...

for background ovveride WM_ERASEBKGND.
for font try CWnd::SetFont()
mdkhangaAuthor Commented:

I need a more complete answer that discusses the commonly encountered issues of customizing a control by inheriting from a control class and overriding the message handler functions.

For my customized control, I should be able to say change the font, the background color, text foreground and text background color.

It is possible that more that one message handler may have to be overriden.

I will give points to the expert who explains the concepts.

No points for answers that say try this or try that.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.