Scrollable List of Buttons


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

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



  • Bigger font size and images in the buttons

    Posted by ptolomeoo on 10/21/2008 06:29pm

    Hello, I am a completely noob. I wanted to put a bigger font size in the buttons, and insert a little icon on a side of those buttons but couldn't figure out how to do that. Could anyone throw a hint on this issue? Thanks in advance.

    • I could figure out about the font size...

      Posted by ptolomeoo on 10/21/2008 06:53pm

      Hello. I could figure out how to increase the font size :-) Just doin: (pCell->lfFont).lfHeight=30; in this function (in GridCtrl.cpp) CGridCell* CGridCtrl::CreateCell(int nRow, int nCol) { CGridCell* pCell = new CGridCell; if (!pCell) return NULL; // Make format same as cell above if (nRow > 0 && nCol >= 0 && nCol < m_nCols) pCell->nFormat = GetItemFormat(nRow-1, nCol); // Make font default grid font memcpy(&(pCell->lfFont), &m_Logfont, sizeof(LOGFONT)); (pCell->lfFont).lfHeight=30; return pCell; }

  • 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