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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…

828 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