Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Cool Push Menu Button

0.00/5 (No votes)
3 Jul 2003 1  
This article shows the use of a Push button with a drop down menu, similar to the one found in the Office 2000 suite.

Sample Image - coolbtn.jpg

Introduction

This article shows the use of a Push button with a drop down menu, similar to the one found in the Office 2000 suite. The code is encapsulated in a MFC class. The class itself is derived from a CButton class, by deriving from this class, most of the button behavior is supplied.

The main core of the code is the drawing routine draw the button and its behavior. Also, there is extensive use of mouse trapping code.

The public interface to access the class is shown here...

BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, 
                  const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL SetButtonImage(UINT nResourceId, COLORREF crMask);
BOOL AddMenuItem(UINT nMenuId,const CString strMenu, UINT nFlags);

These functions create the class, set the bitmap for the button and add menu items for the drop menu menu.

The bitmap must be added as a bitmap resource in the class and it's probably best to use the standard toolbar button size (16x15 pixels).

The color reference for the mask is usually RGB(255,0,255) (Magenta).

Menu items are added to the menu button by the AddMenuItem function. You'll need to create a resource symbol and use the relevant menu flags found in the Windows API under menus (beyond the scope of this document). Command handlers are then added to the parent windows message map (see the code example).

In the sample shown there, the button is constructed in the OnInitDialog handler. The button is created as a Window, but could quite easy be changed to a subclassed dialog item.

There is room for improvement in the class; RemoveMenuItem, ModifyMenuItem could be added and the SetButtonImage could be modified to replace a current image, unfortunately project deadlines prevent me from adding this functionality.

History

  • 29th July, 2000 - Updated source and demo
  • 4th July, 2003 - Update source and demo

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.

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