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!

Downloads

Download source - 3 Kb


Comments

  • Text issue fixed

    Posted by OutsUK on 01/07/2010 08:17am

    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...
    
    dc.SelectObject(pOldFont);
    
    ...during cleanup.
    
    Cheers,
    OutsUK.

    Reply
  • Solution for problem with checkbox and clicks inside

    Posted by leecher on 04/23/2007 07:08pm

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

    Reply
  • there's a problem

    Posted by Legacy on 04/24/2003 12: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
    

    Reply
  • Demo???

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

    Originally posted by: Slowman

    I think a demo is more helpful.

    Reply
  • visible checkbox !?

    Posted by Legacy on 04/11/2001 12: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);
    ModifyStyle(0, (BS_OWNERDRAW|BS_GROUPBOX)&(!WS_VISIBLE));

    CButton::PreSubclassWindow();
    }

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

    know anybody how i can change the text color ?

    Reply
  • Problem with overlaying buttons

    Posted by Legacy on 12/19/2000 12: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.

    Reply
  • Enabling / Disabling problem

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

    Originally 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 ?

    Reply
  • Thanks for this very useful code

    Posted by Legacy on 02/17/2000 12: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.

    Reply
  • Other question...

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

    Originally posted by: Eom Jae-Hong

    Could I get your example workspace source code ?
    
    

    and..

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

    like you used in example image...

    Many Thanks,

    -Eom

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds