Click here to Skip to main content
Click here to Skip to main content

A Button Group Control

, 12 May 2001
Rate this:
Please Sign up or sign in to vote.
An article showing a button group control used to create toolbar-type groups as seen in the Visual Studio options dialogs.

Demo dialog without border around control Demo dialog with border around control

Introduction

This control is a static control that creates a simplified toolbar similar to those found in Visual Studio. As the demo application shows, (as do the pictures above), the control creates a series of buttons within a static control. These buttons can be enabled or disabled, and the control can be drawn with or without a static edge.

Using the Control

It is very easy to put this control into your project. Here's an example:

  1. Add the files btnST.cpp, btnST.h, ButtonGroupCtl.cpp and ButtonGroupCtl.h to your project. These are found in the source download.
  2. Create or open a dialog resource to which you would like to add a button group.
  3. Insert a static control with the appropriate text (‘Options' in the case of the demo). If you would like a border around the group then set the ‘Static Edge' flag:

    Dialog resource editor

  4. In ClassWizard for your dialog class, create a member variable that links to the control, and choose the CButtonGroupCtl class:

    Adding a member variable

    If this does not appear, choose CStatic and then edit your class definition to use CButtonGroupCtl instead.

    Also in your class definition, add the following line:

    #include "ButtonGroupCtl.h"
    

  5. Add a CImageList variable to your class, and initialise this in the constructor of you dialog class. For an example of this, see the demo project.
  6. To add buttons to the control, in your constructor, use the AddButton function:

    void AddButton(int nID, int nImage, LPCTSTR pszToolTip)
    

    nID is the ID of the control you wish to add, for instance IDC_NEW.
    nImage is the index of the image from the ImageList you wish to use.
    pszToolTip is the tooltip you wish to use for the control. You may leave this blank.

    Note that you cannot add or remove buttons from the control once it has been subclassed – it is not intended as a replacement for the toolbar control.

  7. Now, add message handlers for the buttons in your dialog class. You must do this manually by first adding a function (e.g. OnAdd()) to your class:

    Adding a member function as a message hander

    You should then add an entry in the message map, for instance:

    ON_BN_CLICKED(IDC_NEW, OnAddItem)
    

Optionally, you can use the EnableButton(nID, bEnable = true) function to enable and disable buttons at run-time. Note that you cannot call this function until the control has been subclassed. If you want buttons to be initially enabled or disabled, you should do this in your OnInitDialog() override.

I hope this article has helped – the class is relatively simple and basic, but this was the aim. I often have come across the problem where items in a list need to be managed but have not had space to have full-blown buttons with text etc. to do this, so wrote this class to solve that problem. I also felt it was a fairly simple user interface concept to understand because of its use in other programs and also because of its similarity to a toolbar control. I may later update it with the ability to have its contents changed after it has been subclassed although I have not come across a need for this yet myself.

Acknowledgements

I would like to thank Davide Calabro (davide_calabro@yahoo.com) for his CButtonST class.

History

13 May 2001 - updated source files

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Andrew Peace

United Kingdom United Kingdom
Andrew is currently a student at the University of Cambridge, working towards a degree in Computer Science. The word 'working' is used here in a vague sense, with the hope that the reader will realise that the same sentence contained the word 'student'.
 
Aside from computing he has a strong interest in music, and enjoys the outdoors, particularly when the weather permits going out in them... To Andrew, cycling is fun, sailing is more fun, and the odd camping trip and walk is certainly what the doctor ordered.
 
In terms of programming experience, he first started writing programs for the Commodore Amiga using BASIC, after which he learned the joys of C and has never looked back. Since, he has added C++ and C# to his repotoire, along with a bunch of other crazy languages like ML that they like to teach in college.

Comments and Discussions

 
GeneralYet another case... PinmemberHachiihcaH25-Mar-06 9:33 
Questionhow to use functional keys as hotkeys Pinmemberiipc13-Jul-04 20:06 
GeneralVery Nice PinmemberOld Timer1-Oct-02 23:13 
GeneralCool PinmemberTodd Hoop27-Mar-02 3:04 
GeneralNice. One comment PinmemberJerry Evans10-May-01 2:14 
GeneralRe: Nice. One comment PinmemberAndrew Peace14-May-01 13:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 13 May 2001
Article Copyright 2001 by Andrew Peace
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid