Click here to Skip to main content
15,881,687 members
Articles / Desktop Programming / MFC

Cool Push Menu Button

Rate me:
Please Sign up or sign in to vote.
3.39/5 (24 votes)
3 Jul 2003 470K   6.8K   103   83
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...

C++
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.


Written By
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom




Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.


We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net


Visit Software Kinetics

Comments and Discussions

 
GeneralRe: need splitbtn.h Pin
George17-Nov-01 10:31
George17-Nov-01 10:31 
GeneralMenu Pin
MAXS72U5-Sep-01 1:26
MAXS72U5-Sep-01 1:26 
GeneralRe: Menu Pin
NormDroid18-Sep-01 22:15
professionalNormDroid18-Sep-01 22:15 
GeneralSmall improvement Pin
Ernest Laurentin27-Jul-01 10:58
Ernest Laurentin27-Jul-01 10:58 
GeneralRe: Small improvement Pin
NormDroid18-Sep-01 22:13
professionalNormDroid18-Sep-01 22:13 
GeneralDefault Button Style Pin
Derek Lakin26-Jul-01 2:34
Derek Lakin26-Jul-01 2:34 
GeneralRe: Default Button Style Pin
31-Jul-01 2:53
suss31-Jul-01 2:53 
GeneralRe: Default Button Style Pin
31-Jul-01 2:53
suss31-Jul-01 2:53 
GeneralRe: Default Button Style Pin
Derek Lakin31-Jul-01 3:27
Derek Lakin31-Jul-01 3:27 
GeneralRe: Default Button Style Pin
NormDroid31-Jul-01 3:30
professionalNormDroid31-Jul-01 3:30 
GeneralRe: Default Button Style Fix Pin
Derek Lakin31-Jul-01 7:31
Derek Lakin31-Jul-01 7:31 
GeneralRe: Default Button Style Fix Pin
13-Sep-01 0:18
suss13-Sep-01 0:18 
GeneralRe: Default Button Style Fix Pin
Derek Lakin13-Sep-01 0:36
Derek Lakin13-Sep-01 0:36 
GeneralRe: Default Button Style Fix Pin
24-May-02 4:44
suss24-May-02 4:44 
GeneralRe: Default Button Style Fix Pin
Derek Lakin6-Jun-02 8:31
Derek Lakin6-Jun-02 8:31 
GeneralNeed to click again to get off the button Pin
15-Jul-01 22:29
suss15-Jul-01 22:29 
QuestionHow do I convert a dlg button into a cool button Pin
3-Jul-01 20:37
suss3-Jul-01 20:37 
QuestionHow do I convert a dlg button into a cool button Pin
3-Jul-01 20:37
suss3-Jul-01 20:37 
AnswerRe: How do I convert a dlg button into a cool button Pin
11-Sep-01 23:32
suss11-Sep-01 23:32 
GeneralSpeed and Unwanted click Pin
Matt Philmon9-Mar-01 17:17
Matt Philmon9-Mar-01 17:17 
GeneralRe: Speed and Unwanted click Pin
Matt Philmon9-Mar-01 17:38
Matt Philmon9-Mar-01 17:38 
GeneralRe: Speed and Unwanted click Pin
Purna17-Apr-01 5:52
Purna17-Apr-01 5:52 
GeneralRe: Speed and Unwanted click Pin
25-Oct-01 23:20
suss25-Oct-01 23:20 
GeneralThere are bugs in DrawItem Pin
Changhong Pan27-Nov-00 7:23
Changhong Pan27-Nov-00 7:23 
GeneralRe: There are bugs in DrawItem Pin
NormDroid13-Dec-00 2:38
professionalNormDroid13-Dec-00 2:38 

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.