Click to See Complete Forum and Search --> : How to change the background color??


April 10th, 1999, 05:47 AM
Hi,
I am unable to change the background color of a window.
Please help me out of this.

Jerry Coffin
April 10th, 1999, 07:08 PM
First let me advise against doing this at all. The user sets their preferred background color in the control panel, and you're going against their expressed wishes when/if you use another color.

If you insist on doing this anyway, you simply draw a rectangle the size of the window's client area in response to the WM_ERASEBKGND message, filled the color you want.

Let me reiterate: this is usually a BAD idea. Don't do it unless you really have a good reason to think the usual rules don't apply to you in this particular case.


The universe is a figment of its own imagination.

Ajay Kalra
April 10th, 1999, 08:50 PM
You want to do it in handler of WM_ERASEBKGND. Override CWnd::OnEraseBkgnd()
to do it in MFC.

Look at following article on MSDN:
HOWTO: Change Window Background Color with Foundation Classes
Last reviewed: May 28, 1997
Article ID: Q103786

If you are doing it for a MDI main window, you will have to do this for
MDIClient area. Look at:

HOWTO: SubClass the MDIClient by Using MFC
Last reviewed: July 10, 1997
Article ID: Q129471

This article also shows how to erase the background of MDI client area

If you are doing it for controls, you have to handleOnCtlColor(WM_CTLCOLOR).
Look at following article:

HOWTO: Change the Color of an MFC Child Control Class
Last reviewed: June 26, 1997
Article ID: Q132080

Another relevant MSDN article:
HOWTO: Change the Background Color of an MFC Edit Control
Last reviewed: March 2, 1998
Article ID: Q117778


Ajay

Ajay Kalra
April 10th, 1999, 08:56 PM
Its really not as drastic as you portray it to be. There are several scenarios that require a different color:

1.What if a user wants to have windows of different color.

2. You may want to display a state of data visually. In an edit control, enter wrong data etc.

3. Just for fun to see how it looks/works. Not all apps have to be shipped to users.

Ajay