65.9K
CodeProject is changing. Read more.
Home

WTL Button Menu Class

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.76/5 (7 votes)

Nov 6, 2002

viewsIcon

87143

downloadIcon

2173

Add a button that displays a menu when clicked to your WTL application

Sample Image - WTLButtonMenu.jpg

Introduction

A small CButton-derived class that will display a popup menu when clicked. Easy to use and very handy for dialog based application.

Using CButtonMenu

To use this class with a WTL dialog, first include the header file in the MainDlg.h file:

#include "buttonmenu.h"

Next, add a CButtonMenu object to the class:

CButtonMenu m_wndButtonMenu;

Next (and this is important), add a REFLECT_NOTIFICATIONS() call to your dialogs message map (else the button won't get the BN_CLICKED notification message):

BEGIN_MSG_MAP(CMainDlg)
    ...
    REFLECT_NOTIFICATIONS()
END_MSG_MAP()

Now subclass an existing button control and set the popup menu that you want displayed in your dialogs OnInitDialog function:

// Create button menu

m_wndButtonMenu.SubclassWindow(GetDlgItem(IDC_BUTTON1));
// Set the menu ID that the button displays

m_wndButtonMenu.SetMenu(IDR_POPUP);

Finally, add the necessary COMMAND_ID_HANDLERs for your popup menu commands.

That's it! The button will be displayed with an arrow (using the Marlett font if available), and will display a popup menu when clicked.

Notes

When designing a dialog-box that uses this class, instead of using a normal button control, use a checkbox control that has the "Push like" style-bit set. The advantage of using a checkbox is that the button will stay pushed whilst the popup menu is displayed (see the example project for more details)