Scrollable List of Buttons

Sample Image

Environment: VC6 SP2, NT4, Win95

This class allows you to create a scrollable list of buttons. It is
derived from Chris Maunder’s excellent
Grid Control
which appears here on the CodeGuru site. Note that users of this control
are bound by Chris’ copyright requirements detailed on his web page.

My application has a feature which allows a user
to define one or more “commands”. The commands become buttons whose numbers
can grow without limit.
My users prefer this scrollable list of buttons
over a combobox and “go” button implementation (often see on a Web sites) because:

  • It’s less clicking
  • They can see more than one command at a time

Don’t fret too much about using an entire grid implementation to do
something as trivial as a list of buttons. The grid’s executable
footprint is quite small. In addition, if you would like to change
fonts, change text colors, add an image to the button, etc. you can call
the appropriate grid functions to make this happen.

To Use

Get CGridCtrl Source. Create a new project in VC++. Create a new subdirectory within
the project folder named “GridCtrl”. Download the latest version of the
Grid Control and place all
source files within your new GridCtrl folder. Add these files to your VC++ project.

Add Button List Source. Download the button list
source files and add to the VC++ project.

Add the Button List Control to your Dialog. Use VC++ dialog editor to create
a custom control whose class name is: MFCGridCtrl.

Edit your Dialog Header File:

        #include "BtnListCtrl.h"

    // Dialog Data

        CBtnListCtrl m_BtnListCtrl;

Edit your Dialog Implementation File:

    // register the control per CGridCtrl requirements

        void CMyDlg::DoDataExchange(CDataExchange* pDX)
            DDX_GridControl(pDX, IDC_BTNLIST, m_BtnListCtrl);

    // add message handling for button clicks

        BEGIN_MESSAGE_MAP(CMyDlg, CDialog)

        void CMyDlg::OnBtnClick( NMHDR* pNMHDR, LRESULT* pResult)
            GV_DISPINFO *pgvDispInfo = (GV_DISPINFO *)pNMHDR;
            GV_ITEM     *pgvItem = &pgvDispInfo->item;

            if( pgvItem->row > -1 ) // -1 indicates clicked on control but not button
                TRACE( "Clicked Btn Row=%i", pgvItem->row);
            *pResult = 0;

    // Initialize the button list class

        BOOL CMyDlg::OnInitDialog()

            // TODO: Add extra initialization here

            return TRUE;  // return TRUE  unless you set the focus to a control

    // set the button labels anytime after initialization

        CStringArray StringArrayLabels;
        StringArrayLabels.Add( "Btn Label");
        m_BtnListCtrl.WriteButtonLabels( StringArrayLabels);


Download demo project (includes exe and Grid source)- 109 Kb

Download source – 4 Kb


More by Author

Must Read