Transparent Group Box Control


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


Environment: Windows NT4 SP4 (German version), Visual C++ 6 SP2

If you make a dialog with a bitmap as background, you'll probably handle WM_CTLCOLOR + CTLCOLOR_STATIC to set a transparent background for text items.
This works fine except with groupboxes: if the text is transparent the group rectangle goes right through your text, which looks BAD.

To make it look better, use this class CTGroupBox. It draws every side of the box rectangle using LineTo's and takes care of the text position and extent.

Usage is simple: just give your group boxes unique IDs (instead of IDC_STATIC) and then subclass them in OnInitDialog:

// in your .cpp file
void CMyBitmappedDialog::OnInitDialog()
 // I'm using Joerg Koenigs CBitmapDialog class 
 // (see Dialog section of CodeGuru for more info)

 m_TGrpBox1.SubclassDlgItem(IDC_GRPBOX1, this);
 m_TGrpBox2.SubclassDlgItem(IDC_GRPBOX2, this);
 return TRUE;
// in your .h file
#include "TGroupBox.h"

class CMyBitmappedDialog : public CBitmapDialog
 CTGroupBox m_TGrpBox1;
 CTGroupBox m_TGrpBox2;
 // ...

Thats all!

Note: Group boxes are technically buttons(!) with style BS_GROUPBOX but they don't seem to receive WM_DRAWITEMs even if BS_OWNERDRAW is set. So I do the drawing in an WM_PAINT handler but thats not too hard here because they can't be "pressed" or get the focus rect.

Comments, suggestions, bug reports etc. are welcome! Put them here as comment or send them directly to me.



Download source - 3 Kb


  • Text issue fixed

    Posted by OutsUK on 01/07/2010 04:17pm

    I was hitting an issue with the wrong font being picked up when drawing.  This is fixed by calling...
    CFont* pOldFont = dc.SelectObject(GetFont());
    ...before checking the text size and then...
    ...during cleanup.

  • Solution for problem with checkbox and clicks inside

    Posted by leecher on 04/24/2007 02:08am

    You have to process the following message to get rid of the checkbox appearing on click and the problem with clicking inside the groupbox:
    Hope that helps.

  • there's a problem

    Posted by Legacy on 04/24/2003 07:00am

    Originally posted by: lincy

    there's a problem when retrieving value from the date time picker control to the next form when used as COleDateTime

  • Demo???

    Posted by Legacy on 11/07/2001 08:00am

    Originally posted by: Slowman

    I think a demo is more helpful.

  • visible checkbox !?

    Posted by Legacy on 04/11/2001 07:00am

    Originally posted by: peter rumps

    when i use this sources i get a checkbox in the middle of the groupbox when i click on it.

    with the following change it work better:

    void CTGroupBox::PreSubclassWindow()
    // ModifyStyle(0, BS_OWNERDRAW|BS_GROUPBOX);


    but now i have the problem to change the color of the text.

    know anybody how i can change the text color ?

  • Problem with overlaying buttons

    Posted by Legacy on 12/19/2000 08:00am

    Originally posted by: Marko Kaiser

    Your Groupbbox class is very useful. But on my project an silly error occurs:
    When I use the groupbox to group a static and an edit control and a button, clicking the inside button does not work. I used ON_BN_CLICKED abd ON_COMMAND Macro, but the only thing thatworked well was to use accelerator key for the button otherwise a checkbox occured somewhere on the dialog. What is wrong ? I can�t figure it out.

  • Enabling / Disabling problem

    Posted by Legacy on 07/31/2000 07:00am

    Originally posted by: Mike Marquet


    I have a problem when I enable or disable the groupbox.
    The problem is that when I disable the groupbox, all items in the group are hidden but accesible at an other place. When I enable the groupbox, all items within the group are not shown again. I must swap to an other application I come back to see inside controls.
    When I send a InvalidateRect(NULL) after EnableWindow(TRUE), it doesn't work.

    Any idea ?

    P.S. : Can you post me your sample source project ?

  • Thanks for this very useful code

    Posted by Legacy on 02/17/2000 08:00am

    Originally posted by: Toby Norris

    Your code will serve as a great starting point for a more sophisticated group box that I need. I want a group box that allows the user to control the color, thickness, etc. for the group box lines as well as the font, color, etc. of the text. But most of all I want the control to have the ability to change colors (or highlight) the box lines, text and contained controls when the cursor enters the group box rectangle. Thanks for your very useful code.

  • Other question...

    Posted by Legacy on 02/08/2000 08:00am

    Originally posted by: Eom Jae-Hong

    Could I get your example workspace source code ?


    I want to know... how make transparent buttons...

    like you used in example image...

    Many Thanks,


  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date