Changing Font/appearance of  MFC controls

Posted on 1998-11-29
Last Modified: 2013-12-03
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 ?
Question by:mdkhanga

Expert Comment

ID: 1325470
for background ovveride WM_ERASEBKGND.
for font try CWnd::SetFont()

Author Comment

ID: 1325471

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.


Accepted Solution

Answers2000 earned 100 total points
ID: 1325472
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...


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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

770 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