Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Changing Font/appearance of  MFC controls

Posted on 1998-11-29
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 400 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: 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

610 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