Custom Draw Buttons '& A smarter Groupbox


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

What's in this Code?

1. CRadioGroup: A group box class that handles radio buttons as an array

Sometimes when you have a number of radio buttons in a group you don't want to deal with them individually. CRadioGroup is a simple class that allows you to access any radios bounded by a groupbox as a 1 based array.

Class hierarchy:

How to use the CRadioGroup?

To use the CRadioGroup class simply subclass a Groupbox in a dialog and use the SetRadio() and GetRadio() methods to access the currently selected radio button. I have made the accessing 1 based ( 0 based arrays have always upset me), but this can be easily changed.
When used in conjunction with CCustomButton the CRadioGroup will also receive a BN_CLICKED message whenever a contained radio is clicked. Thus only one handler is required for any number of  radio buttons inside a group box.

Subclassing the groupbox:

void CMyDialog::DoDataExchange(CDataExchange* pDX)

    DDX_Control(pDX, IDC_GROUP1, m_Group1);
Handling hits on the radio buttons via the groupbox (NB. The radio buttons must be CCustomButton's or derived from a CCustomButton) :


The code for CRadioGroup is in CRadioGroup.cpp/.h  (See source.)

How does the CRadioGroup work?

The class simply steps through the auto-radio buttons that are bounded by it and finds the currently selected one. If this is the second auto-radio button then GetRadio() will return 2. Conversely SetRadio(2) will select this one, deselecting the currently selected one. Simple.

2. CCustomButton: A generic user draw button

When I started writing MFC dialog code I thought it would be easy to override the drawing of buttons. Foolish me!
All I wanted was to draw the buttons  based on the type and current state while retaining the button behaviour. After some trial and error I created a class that allowed standard button behaviour but with the option of the user handling all  drawing.
I have included a sample implementation of a CCustomButton in CCustomIconButton which takes an 'icon' from a toolbar style strip bitmap and draws it in a button frame. ( See the picture above).

Class hierarchy:

How do you use the CCustomButton &  CCustomIconButton ?

1. To use CCustomButton simply override the following 2 methods :

virtual BOOL IsCustom()const {return TRUE;} // Return TRUE so that Draw() is called
virtual void Draw(CDC*,CRect,const CCustomButtonData& state)
   //Draw the button based on state flags in state
2. CCustomIconButton is a simple sample of what can be done using the CCustomButton. To use it

(a). Subclass your button with a CCustomIconButton.
(b). In OnInitDialog() call:  myButton.LoadIcon(), Passing the ID of a strip bitmap, the offset of the button         image (again 1 based) and the size of the button image.

eg. myButton.LoadIcon(IDB_MYBITMAP,nOffset,CSize(24,23));

How do the CCustomButton and CCustomIconButton classes work?

The CCustomButton overrides all of the methods and message handlers that are needed to prevent the underlying button drawing itself. If the IsCustom() method returns FALSE then the button will look and behave as normal. But if IsCustom() returns TRUE then the Draw() method is called for drawing the button. The current button state is passed to the Draw() method via the CCustomButtonData class.
The code for CCustomButton is in CCustomButton.cpp/.h
The code for CCustomIconButton is in CCustomIconButton.cpp/.h
For more information, please refer to the comments in the source code.

Download source and sample - 24 KB


  • There are no comments yet. Be the first to comment!

  • 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