Transparent Group Box Control

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)
CBitmapDialog::OnInitDialog();
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
{
private:
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.
Enjoy!

Comments
Text issue fixed
Posted by OutsUK on 01/07/2010 08:17amSolution for problem with checkbox and clicks inside
Posted by leecher on 04/23/2007 07:08pmthere's a problem
Posted by Legacy on 04/24/2003 12:00amOriginally posted by: lincy
ReplyDemo???
Posted by Legacy on 11/07/2001 12:00amOriginally posted by: Slowman
I think a demo is more helpful.
Reply
visible checkbox !?
Posted by Legacy on 04/11/2001 12:00amOriginally posted by: peter rumps
ReplyProblem with overlaying buttons
Posted by Legacy on 12/19/2000 12:00amOriginally posted by: Marko Kaiser
Your Groupbbox class is very useful. But on my project an silly error occurs:
ReplyWhen 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 12:00amOriginally posted by: Mike Marquet
Hi,
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 ?
ReplyThanks for this very useful code
Posted by Legacy on 02/17/2000 12:00amOriginally 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.
ReplyOther question...
Posted by Legacy on 02/08/2000 12:00amOriginally posted by: Eom Jae-Hong
Reply