Background Color of GroupBox

Posted on 2000-02-23
Last Modified: 2013-11-20
I have a group box and i want to sent the BackGround color of the Group box as white ,i tried to trap the WM_CTLCOLOR for it ,it doesn't there any simple way to work it out..
Question by:bhoomi_2000
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
  • 4
  • 2
  • 2
  • +1

Expert Comment

ID: 2551794
U have to derive UR class and map WM_CTLCOLOR to it and UR group box varible should be of that class type
LVL 11

Expert Comment

ID: 2552097
Are you trying to cange the background color of the interior of the group box?

..B ekiM

Author Comment

ID: 2552271
Yeah i have my own derived Group Box,tried to MAP the WM_CTLCOLOR for it,but unfortunately , i don't get any messages of WM_CTLCOLOR for the derived Group box,and also in the Parent class...
Yeah I am trying to change the background color of the interior of the group box..
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Expert Comment

ID: 2552835
Group boxes don't draw their interiors (AFAIK) .. just the border.

How about you put a white rectangle (static) control inside it.


Author Comment

ID: 2555492
If i place a static control ,white rectangle ,it overshadows other controls and the other controls which i listed in the group box is invisible

Author Comment

ID: 2555493
If i place a static control ,white rectangle ,it overshadows other controls and the other controls which i listed in the group box is invisible
LVL 11

Accepted Solution

mikeblas earned 30 total points
ID: 2555618
A group box control draws its interior, just like most every other control in the world.

1) Create your own class with ClassWizard.  Call it, for example, CMyGroupBox. Base it on CStatic.

2) Add a handler for WM_ERASEBKGND to your class. Code something that paints the color you want. For example, you can do this:

BOOL CMyGroupBox::OnEraseBkgnd(CDC* pDC)
   CRect rect;

   CBrush brushBlue(RGB(0, 0, 255));
   CBrush* pOldBrush = pDC->SelectObject(&brushBlue);


   return TRUE;

3) Add an instance of CMyGroupBox to your dialog as member data.  For instance,

   CMyGroupBox m_box;

4) In OnInitDialog() of your dialog box, subclass the control. If your control's ID is IDC_MYBOX, then use:

   m_box.SubclassDlgItem(IDC_MYBOX, this);  

Note that you must have a real ID; you can't use IDC_STATIC.

That's it. Now, that OnEraseBkgnd() handler will paint the backgorund of the box.

Note that you might want to also handle WM_CTLCOLOR for the control in order to have the background of the label text (or even the text colour) look nice. Also, note that most controls paint opaque--so you might have to handle WM_CTLCOLOR for controls inside the box and set transparent mode on them to let your new background color show thru.

..B ekiM
LVL 10

Expert Comment

ID: 2556170
Mike: Doesn't a group box paint its interior with a hollow (or maybe null) brush?  If so (unless one is being pedantic), the group box effectively doesn't do anything to its interior (paints it with nothing).  That is the point I was trying to make.  Obviously I didn't succeed :-).

bhoomi: Whether or not the rectangle is under or over other control is a matter of dialog layout order.  You can easily control this yourself (Ctrl+D in resrouce/dialog editor).

Mike's suggestion seems to be the best.  Good idea Mike .. didn't think of deriving and doing the OnEraseBkgnd, although I've done similar things many times myself.

bhoomi: might also be worth your while looks at an MSJ article (May 1997 C/C++ Q & A) by Paul D'Lascia where he shows how to put a bitmap background on a dialog, and discusses all the ins and outs of how to make controls appear correctly over a non-standard background (which is what you'll need to do to make your white background look correct).


Author Comment

ID: 2558619
I think Ron and mikes both of your suggestion has to be combined for effective solution.But mike the other best way is to derive a own CStatic class and trap WM_CTLCOLOR message and paint the static control with required brush.Instead of handling WM_ERASEBACKGROUND,your suggestion also is an good alternative.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 92
Whole sheet autoscrub still needed 19 55
Line meaning 9 99
Problem to Office 1 68
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: 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.
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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