Click here to Skip to main content
11,644,286 members (71,423 online)
Click here to Skip to main content

Cool Push Menu Button

, 3 Jul 2003 405.7K 5.8K 99
Rate this:
Please Sign up or sign in to vote.
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 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

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

Share

About the Author

NormDroid
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

You may also be interested in...

Comments and Discussions

 
GeneralSmall Modification Pin
ChasCurry27-Jan-11 8:17
memberChasCurry27-Jan-11 8:17 
GeneralMy vote of 4 Pin
Smaaart5-Oct-10 19:14
memberSmaaart5-Oct-10 19:14 
GeneralMy vote of 1 Pin
Sheetal_Joshi22-Oct-09 2:18
memberSheetal_Joshi22-Oct-09 2:18 
GeneralRe: My vote of 1 Pin
Norm .net28-Oct-09 1:06
groupNorm .net28-Oct-09 1:06 
GeneralRe: My vote of 1 Pin
Sheetal_Joshi8-Dec-09 5:48
memberSheetal_Joshi8-Dec-09 5:48 
GeneralRe: My vote of 1 Pin
Norm .net8-Dec-09 20:44
groupNorm .net8-Dec-09 20:44 
GeneralMy vote of 1 Pin
Ryleigh27-Mar-09 7:37
memberRyleigh27-Mar-09 7:37 
GeneralRe: My vote of 1 Pin
Rajesh R Subramanian20-May-09 3:44
mvpRajesh R Subramanian20-May-09 3:44 
GeneralRe: My vote of 1 Pin
Norm .net20-May-09 4:44
groupNorm .net20-May-09 4:44 
GeneralMy vote of 1 Pin
Elkay9-Jan-09 6:37
memberElkay9-Jan-09 6:37 
GeneralMislocation of the button Pin
Carrie_Jia22-Jun-05 17:07
memberCarrie_Jia22-Jun-05 17:07 
GeneralMinor Modifications Pin
DaemonNZ27-Nov-03 12:44
memberDaemonNZ27-Nov-03 12:44 
GeneralA few additions... Pin
Anonymous7-Nov-03 6:21
sussAnonymous7-Nov-03 6:21 
Question.NET Version for WindowsForms? Pin
Uwe Keim25-Aug-03 0:10
sitebuilderUwe Keim25-Aug-03 0:10 
GeneralNew version Pin
Hans Dietrich7-Jul-03 2:19
memberHans Dietrich7-Jul-03 2:19 
GeneralRe: New version Pin
Hans Dietrich7-Jul-03 4:38
memberHans Dietrich7-Jul-03 4:38 
GeneralRe: New version Pin
Normski17-Jul-03 21:16
memberNormski17-Jul-03 21:16 
GeneralRe: New version Pin
Hans Dietrich18-Jul-03 7:45
memberHans Dietrich18-Jul-03 7:45 
Generalnot descriptive!! Pin
M.Shoaib Khan5-Jul-03 7:28
memberM.Shoaib Khan5-Jul-03 7:28 
GeneralRe: not descriptive!! Pin
Normski17-Jul-03 6:47
memberNormski17-Jul-03 6:47 
GeneralA better solution Pin
Suha Aktan10-Jun-03 21:23
memberSuha Aktan10-Jun-03 21:23 
GeneralRe: A better solution Pin
CodeCracker11-Jun-03 9:30
memberCodeCracker11-Jun-03 9:30 
GeneralRe: A better solution Pin
Suha Aktan12-Jun-03 3:25
memberSuha Aktan12-Jun-03 3:25 
GeneralRe: A better solution Pin
Normski16-Jun-03 8:34
memberNormski16-Jun-03 8:34 
GeneralRe: A better solution Pin
veal14-Jun-03 2:58
memberveal14-Jun-03 2:58 
GeneralCCoolButton works with XP; additional features Pin
leighb10-Jun-03 14:07
memberleighb10-Jun-03 14:07 
GeneralRe: CCoolButton works with XP; additional features Pin
CodeCracker11-Jun-03 9:31
memberCodeCracker11-Jun-03 9:31 
QuestionDriven from keyboard? Pin
Carl Young1-Apr-03 3:39
memberCarl Young1-Apr-03 3:39 
Generalhelp desperately needed for Button menu.. Pin
*S*2-Feb-03 9:19
suss*S*2-Feb-03 9:19 
Generalvery good Pin
yary19-Dec-02 1:31
memberyary19-Dec-02 1:31 
GeneralPossible improvement Pin
__Stephane Rodriguez__16-Oct-02 5:13
member__Stephane Rodriguez__16-Oct-02 5:13 
GeneralAnother small improvement Pin
Cabbi28-Aug-02 21:58
sussCabbi28-Aug-02 21:58 
Generalhelp Pin
Anonymous2-Jun-02 18:13
memberAnonymous2-Jun-02 18:13 
Generalextra semicolon Pin
Anonymous5-May-02 22:22
memberAnonymous5-May-02 22:22 
GeneralRe: extra semicolon Pin
Norm Almond16-May-02 11:12
memberNorm Almond16-May-02 11:12 
GeneralRe: Cool Push Menu Button Pin
nkshastry200015-Feb-06 4:04
membernkshastry200015-Feb-06 4:04 
GeneralRe: Cool Push Menu Button Pin
norm.net15-Feb-06 4:26
membernorm.net15-Feb-06 4:26 
GeneralRe: extra semicolon Pin
Shielsy104-Jul-03 7:57
memberShielsy104-Jul-03 7:57 
GeneralCan't seem to add bitmaps to submenus Pin
Julia Larson26-Apr-02 10:46
memberJulia Larson26-Apr-02 10:46 
GeneralNevermind, I'm a knucklehead Pin
Julia Larson26-Apr-02 11:19
memberJulia Larson26-Apr-02 11:19 
GeneralRe: Can't seem to add bitmaps to submenus Pin
Anonymous30-May-02 23:52
memberAnonymous30-May-02 23:52 
Generalenable/disable ,remove menu items Pin
amitbamzai11-Apr-02 18:26
memberamitbamzai11-Apr-02 18:26 
Generalenable/disable ,remove menu items Pin
amitbamzai11-Apr-02 18:23
memberamitbamzai11-Apr-02 18:23 
GeneralCommand not generated when the main button pressed Pin
Simon Reeves18-Feb-02 2:59
memberSimon Reeves18-Feb-02 2:59 
GeneralRe: Command not generated when the main button pressed Pin
Norm Almond18-Feb-02 4:11
memberNorm Almond18-Feb-02 4:11 
GeneralRe: Command not generated when the main button pressed Pin
Neville Franks3-Mar-02 21:05
memberNeville Franks3-Mar-02 21:05 
GeneralRe: Command not generated when the main button pressed Pin
Flippant23-Apr-03 4:48
memberFlippant23-Apr-03 4:48 
GeneralBUG! The button is always drawn as a default button Pin
chcw6-Feb-02 13:39
memberchcw6-Feb-02 13:39 
Generalneed splitbtn.h Pin
Anonymous24-Oct-01 2:53
memberAnonymous24-Oct-01 2:53 
GeneralRe: need splitbtn.h Pin
George17-Nov-01 10:31
memberGeorge17-Nov-01 10:31 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 4 Jul 2003
Article Copyright 2000 by NormDroid
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid