Click here to Skip to main content
Licence 
First Posted 3 May 2000
Views 196,811
Bookmarked 59 times

Gradient Menus in MFC

By | 14 May 2000 | Article
Create Popup menus in MFC with a gradient and text on the left side
  • Download source files - 15 Kb
  • Download demo project - 75 Kb
  • Sample Image - Gradient Menus.jpg

    Introduction

    Here's a bit of eye-candy I whipped up to drop into our product: epAssist.

    The program has a system tray icon and a context menu comes up when you click on the icon. One problem with tray icons, it that it can be easy for users to confuse what the menu for your program looks like.

    My first solution was to place a disabled menu item with the application name at the top of the menu, but this didn't have the effect I needed. Upon searching my favorite developer site, I found a few menu classes that could act like the Windows Start Menu.

    I based my menu off these articles and a few others, making as few changes as required to get the effect I needed. The class presented here is by no means original, but I believe that the way the components are put together produces a original and impressive UI component.

    The first and easiest use of this class is to place a context menu on the screen. The sample code below shows how to setup and use the simple interface this menu exposes. There are a few shortcomings. First, the menu does not send Command UI messages for enabling/checking menu items. Secondly, messages are sent to the window that owns the menu. If you look at the sample application, you will notice that the view handles ID_APP_EXIT1 and posts ID_APP_EXIT to the MainFrame. If the view handled ID_APP_EXIT and posted it to the frame, standard MFC command routing would have the view intercepting the message first (again!)

    #include "GradientMenu.h"
    void CGrMenuTestView::OnContextMenu(CWnd* pWnd, CPoint point) 
    {
    	// Set up the colours.. Might want these in program options
    	COLORREF rgb1(RGB(0, 0, 0));
    	COLORREF rgb2(RGB(128, 128, 255));
    	COLORREF rgbText(RGB(255, 255, 255));
    
    	// Create a menu, set the width of gradient
    	CGradientMenu oMenu(24, TRUE);
    	oMenu.LoadMenu(IDR_POPUP_MENU);
    
    	// Select colours and set the title for the 1st menu
    	// sub menus will take the name of the item selected in the parent.
    	oMenu.SetGradientColors(rgb1, rgb2, rgbText);
    	oMenu.SetTitle((CString)"Test");
    
    	// Show the menu!
    	oMenu.TrackPopupMenu(0, point.x, point.y, this);
    
    	// Done!
    }
    

    That's it. You need to give the root menu a title, but the sub-menus will get their title from their parent.

    For the main program menu, I simply faked it out. The main menu has no popups, but handles the WM_COMMAND and puts a popup under the menu item. See the sample for full details.

    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

    Jim Koornneef

    Web Developer
    Golden Crater Corp
    Canada Canada

    Member

    Jim is the President of Golden Crater Corp. (formerly Golden Crater Software) which produces:
     
    Tiny eBook Reader - Read eBooks anywhere, on any web enabled device or phone.
     
    Doberman BMS - Home Automation and Building Management System bridging and enhancing several automation hardware platforms.

    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
    Generalsystray menu PinmemberTapdonce15:40 12 Jul '09  
    QuestionHere is a problem Sir... Pinmemberrootdial23:26 27 Jul '06  
    GradientMenu.cpp: error C2440: 'static_cast': 'void (__thiscall MenuToolBar::* )(LPARAM,LRESULT *)' can not convert to 'void (__thiscall CCmdTarget::* )(NMHDR *,LRESULT *)'
     
    Just a little thing Smile | :)
    Generalmenu PinsussAnonymous12:30 14 Mar '04  
    QuestionHow to set check on menus PinmemberSnillet2k7:01 30 Dec '02  
    AnswerRe: How to set check on menus Pinmemberas_sound_as5:37 10 Jan '07  
    Generalnice.. Pinmemberyary2:21 19 Dec '02  
    Generalfast right click bug PinsussMartin Madreya5:39 24 Oct '02  
    Generalbug report PinmemberXiaolei Zhuansun11:54 3 Dec '01  
    GeneralWish List PinmemberDavid Berman19:31 16 Oct '01  
    QuestionBitmaps Menus? PinmemberJonny Newman6:18 11 Oct '01  
    GeneralProblem with Windows XP.... PinmemberParas1:32 1 Oct '01  
    GeneralRe: Problem with Windows XP.... PinmemberJim Koornneef4:03 3 Oct '01  
    GeneralRe: Problem with Windows XP.... PinmemberJase Jennings11:54 16 Feb '02  
    GeneralMenu is shown during MOVING Dialog PinmemberHamed19:26 19 Sep '01  
    GeneraloMenu.GetSubMenu(0)->TrackPopupMenu(...) does not work PinmemberBrad Epperson19:37 3 Apr '01  
    QuestionNo Colors in SubMenu ? PinmemberElco Grobler1:21 1 Mar '01  
    AnswerRe: No Colors in SubMenu ? PinmemberJim Koornneef15:26 1 Mar '01  
    GeneralMy wislist item for this cool class... PinmemberJason Hattingh3:20 20 Nov '00  
    GeneralRetrieving the command ID(s) PinsussDan Scott6:00 8 Sep '00  
    GeneralRe: Retrieving the command ID(s) PinmemberMazhekin2:34 7 Sep '04  
    Generalvery very nice !!!, but ... Pinsussguy5:13 24 Aug '00  
    GeneralRe: very very nice !!!, but ... PinmemberZulfiqar Inayat Malik0:26 25 Jun '01  
    GeneralGood work PinsussKen Koornneef7:51 9 Aug '00  
    GeneralNot working in VC5 Pinsusssten struikenkamp5:52 19 Jul '00  
    GeneralRe: Not working in VC5 PinsussJim Koornneef2:57 9 Aug '00  

    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 15 May 2000
    Article Copyright 2000 by Jim Koornneef
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid