Click here to Skip to main content
Licence 
First Posted 28 Jun 2000
Views 368,501
Downloads 4,658
Bookmarked 96 times

Cool Push Menu Button

By | 3 Jul 2003 | Article
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

About the Author

Norm .net

Chief Technology Officer
Software Kinetics
United Kingdom United Kingdom

Member

Follow on Twitter Follow on Twitter



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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSmall Modification PinmemberChasCurry8:17 27 Jan '11  
GeneralMy vote of 4 PinmemberSmaaart19:14 5 Oct '10  
GeneralMy vote of 1 PinmemberSheetal_Joshi2:18 22 Oct '09  
GeneralRe: My vote of 1 PingroupNorm .net1:06 28 Oct '09  
GeneralRe: My vote of 1 PinmemberSheetal_Joshi5:48 8 Dec '09  
GeneralRe: My vote of 1 PingroupNorm .net20:44 8 Dec '09  
GeneralMy vote of 1 PinmemberRyleigh7:37 27 Mar '09  
GeneralRe: My vote of 1 PinmvpRajesh R Subramanian3:44 20 May '09  
GeneralRe: My vote of 1 PingroupNorm .net4:44 20 May '09  
GeneralMessage Automatically Removed PinmemberElkay6:37 9 Jan '09  
GeneralMislocation of the button PinmemberCarrie_Jia17:07 22 Jun '05  
Hi,
 
Your work offers a great help in my project. Thank you very much.
However, i faced a problem: mislocation of the button when my project is run and tested on other PC.
 
Scenario:
i created a button in the OnInitialUpdate() and place it on a FormView.
 
RECT stRect;
stRect.left = 0+454;
stRect.top = 0+197;
stRect.right = 454+74;
stRect.bottom = 197+23;
CRect rect1(stRect);
 
m_btn.Create("&Settings", WS_TABSTOP | WS_CHILD |WS_VISIBLE, rect1 ,this,IDC_SETTING);
 
Problem:
When this program is run on other PC, the button is no longer at the place i desired. It jumps to other location! why? Confused | :confused:
 
I've tested on windows resolution, but doesn't solve the prolem.
 
Please advice. Thank you! Big Grin | :-D

GeneralMinor Modifications PinmemberDaemonNZ12:44 27 Nov '03  
GeneralA few additions... PinsussAnonymous6:21 7 Nov '03  
Question.NET Version for WindowsForms? PinsitebuilderUwe Keim0:10 25 Aug '03  
GeneralNew version PinmemberHans Dietrich2:19 7 Jul '03  
GeneralRe: New version PinmemberHans Dietrich4:38 7 Jul '03  
GeneralRe: New version PinmemberNormski21:16 17 Jul '03  
GeneralRe: New version PinmemberHans Dietrich7:45 18 Jul '03  
Generalnot descriptive!! PinmemberM.Shoaib Khan7:28 5 Jul '03  
GeneralRe: not descriptive!! PinmemberNormski6:47 17 Jul '03  
GeneralA better solution PinmemberSuha Aktan21:23 10 Jun '03  
GeneralRe: A better solution PinmemberCodeCracker9:30 11 Jun '03  
GeneralRe: A better solution PinmemberSuha Aktan3:25 12 Jun '03  
GeneralRe: A better solution PinmemberNormski8:34 16 Jun '03  
GeneralRe: A better solution Pinmemberveal2:58 14 Jun '03  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 4 Jul 2003
Article Copyright 2000 by Norm .net
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid