Scrollable List of Buttons | CodeGuru

Scrollable List of Buttons

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 4, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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)
        {
            CDialog::DoDataExchange(pDX);
            //{{AFX_DATA_MAP(CMyDlg)
            //}}AFX_DATA_MAP
            ...
            DDX_GridControl(pDX, IDC_BTNLIST, m_BtnListCtrl);
        }


    // add message handling for button clicks

        BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
            //{{AFX_MSG_MAP(CMyDlg)
            //}}AFX_MSG_MAP
            ...
            ON_NOTIFY(GVN_COLUMNCLICK, IDC_BTNLIST, OnBtnClick)
        END_MESSAGE_MAP()
        ...

        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
            m_BtnListCtrl.InitializeButtons();

            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);

Downloads

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

Download source – 4 Kb

History

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.