Cool Push Menu Button
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 its 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 adding this functionality.
History
- 29 July 2000 - updated source and demo
- 4 July 2003 - update source and demo