Click here to Skip to main content
13,298,655 members (53,501 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 27 Jul 2000

WTL - Button Menu

, 13 Aug 2000
Rate this:
Please Sign up or sign in to vote.
Use WTL to create simple button that implements drop-down menu

Sample Image - ButtonMenu.gif

DevStudio's Tools|Customize menu displays a dialog box that contains a button control with an arrow on it. When pressed, the button will display a drop-down menu. I needed this feature in my ATL applications, but didn't want the overhead of MFC or the headache of Win32. So I borrowed from Norm Almond's excellent example of Cool Push Menu Button and ported it to WTL, simplifying it's functionality along the way. I'm not implying anything was wrong with what Norm developed...the control just did more than I needed it to. I will eventually port his entire control to WTL, when time allows.


CButtonMenu is derived from CButtonMenuImpl. To draw the arrow on the button, CButtonMenuImpl needs to have the BS_OWNERDRAW style set. However, this will send the WM_DRAWITEM msg to the parent of the CButtonMenuImpl, and not the CButtonMenuImpl itself. By subclassing the parent HWND, the CButtonMenuImpl can hijack the msg and handle it alone. To do this, the CButtonMenuImpl has a private member CContainedWindow variable that subclasses the parent. Then, using the ALT_MSG_MAP() macro, we can catch the msg:

class CButtonMenuImpl : ...
CContainedWindow m_Parent;

    {other code}
    typedef CWindowImpl<T, TBase, TWinTraits> baseClass;
        other msgs
        {other code}

Finally, to make simple use of this control, CButtonMenu provides an operator overload:
class CButtonMenu : public CButtonMenuImpl<CButtonMenu>
    DECLARE_WND_SUPERCLASS(_T("WTL_CButtonMenu"), GetWndClassName())
    CButtonMenu& operator=(HWND hWnd)
        if ( m_hWnd )
        if ( ::IsWindow(hWnd) )
        return *this;
Here's an example of how to initialize a CButtonMenu with a button already on a dialog:
// Member variable of dialog
CButtonMenu btnMenu;

// Somewhere in WM_INITDIALOG handler
btnMenu = GetDlgItem(IDC_BTN_ABOUT);
When the user selects a menu item, a msg will be sent to the dialog.


</li />

  • Doesn't provide a Create method that will initialize the control properly (yet)
  • Doesn't respond to the BS_DEFPUSHBUTTON flag


    August 14, 2000 - Fixed the OnDraw method to use the DRAWITEMSTRUCT's HWND instead of the Button's HWND when calling GetWindowText(). This fixed a bug that resulted in two different CButtonMenu's on the same dialog always painting the same caption.
  • 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

    David Peterson
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralNo Button title appearing on Win98 Pin
    d.khan6-Jun-03 0:36
    sussd.khan6-Jun-03 0:36 
    GeneralRe: No Button title appearing on Win98 Pin
    RCube-14-Nov-03 16:56
    memberRCube-14-Nov-03 16:56 
    Generaltext bug Pin
    ljhhome14-Jan-03 20:47
    memberljhhome14-Jan-03 20:47 
    GeneralRe: text bug Pin
    RCube-16-Nov-03 13:02
    memberRCube-16-Nov-03 13:02 
    GeneralMenu off by one pixel Pin
    Bryan Ressler13-Dec-01 10:38
    memberBryan Ressler13-Dec-01 10:38 
    GeneralCaption Pin
    Steffen11-Jul-01 2:47
    memberSteffen11-Jul-01 2:47 
    Generaldebug assertion failed. Pin
    Andla28-Mar-01 6:54
    memberAndla28-Mar-01 6:54 
    GeneralRe: debug assertion failed. Pin
    Anonymous28-Mar-01 8:05
    memberAnonymous28-Mar-01 8:05 
    Don't declare your CButtonMenu object in the routine's scope. Declare it in the dialog's class scope.

    -David Peterson
    GeneralMissing atlutils.h Pin
    Shilp1-Nov-00 8:53
    sussShilp1-Nov-00 8:53 
    GeneralRe: Missing atlutils.h Pin
    SAWilde30-Nov-00 7:06
    memberSAWilde30-Nov-00 7:06 

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

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

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.171207.1 | Last Updated 14 Aug 2000
    Article Copyright 2000 by David Peterson
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid