Solved

Changing Font/appearance of  MFC controls

Posted on 1998-11-29
3
628 Views
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 ?
0
Comment
Question by:mdkhanga
3 Comments
 
LVL 2

Expert Comment

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

Author Comment

by:mdkhanga
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.

0
 
LVL 8

Accepted Solution

by:
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)
http://www.wildcomputer.com/h2winnofadialogcontrol.htm

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,

e.g.
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...


0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
string initialization in java 11 103
has22 challenge 11 72
mapBully challenge 6 92
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now