Click here to Skip to main content
13,446,762 members (40,068 online)
Click here to Skip to main content
Add your own
alternative version


64 bookmarked
Posted 3 May 2000

Gradient Menus in MFC

, 14 May 2000
Rate this:
Please Sign up or sign in to vote.
Create Popup menus in MFC with a gradient and text on the left side
<!-- Link to source file download -->
  • Download source files - 15 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 75 Kb
  • <!-- Article image -->

    Sample Image - Gradient Menus.jpg

    <!-- Add the rest of your HTML here -->


    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);
    	// 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);
    	// 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.<p>


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

    You may also be interested in...

    Comments and Discussions

    Generalsystray menu Pin
    Tapdonce12-Jul-09 15:40
    memberTapdonce12-Jul-09 15:40 
    QuestionHere is a problem Sir... Pin
    rootdial27-Jul-06 23:26
    memberrootdial27-Jul-06 23:26 
    Generalmenu Pin
    Anonymous14-Mar-04 12:30
    sussAnonymous14-Mar-04 12:30 
    QuestionHow to set check on menus Pin
    Snillet2k30-Dec-02 7:01
    memberSnillet2k30-Dec-02 7:01 
    Hi Do you know how to set a checksymbol in a menuitem?
    (I'm a beginner)Confused | :confused:
    AnswerRe: How to set check on menus Pin
    as_sound_as10-Jan-07 5:37
    memberas_sound_as10-Jan-07 5:37 
    Generalnice.. Pin
    yary19-Dec-02 2:21
    memberyary19-Dec-02 2:21 
    Generalfast right click bug Pin
    Martin Madreya24-Oct-02 5:39
    sussMartin Madreya24-Oct-02 5:39 
    Generalbug report Pin
    Xiaolei Zhuansun3-Dec-01 11:54
    memberXiaolei Zhuansun3-Dec-01 11:54 
    GeneralWish List Pin
    David Berman16-Oct-01 19:31
    memberDavid Berman16-Oct-01 19:31 
    QuestionBitmaps Menus? Pin
    Jonny Newman11-Oct-01 6:18
    memberJonny Newman11-Oct-01 6:18 
    GeneralProblem with Windows XP.... Pin
    Paras1-Oct-01 1:32
    memberParas1-Oct-01 1:32 
    GeneralRe: Problem with Windows XP.... Pin
    Jim Koornneef3-Oct-01 4:03
    memberJim Koornneef3-Oct-01 4:03 
    GeneralRe: Problem with Windows XP.... Pin
    Jase Jennings16-Feb-02 11:54
    memberJase Jennings16-Feb-02 11:54 
    GeneralMenu is shown during MOVING Dialog Pin
    Hamed19-Sep-01 19:26
    memberHamed19-Sep-01 19:26 
    GeneraloMenu.GetSubMenu(0)->TrackPopupMenu(...) does not work Pin
    Brad Epperson3-Apr-01 19:37
    memberBrad Epperson3-Apr-01 19:37 
    QuestionNo Colors in SubMenu ? Pin
    Elco Grobler1-Mar-01 1:21
    memberElco Grobler1-Mar-01 1:21 
    AnswerRe: No Colors in SubMenu ? Pin
    Jim Koornneef1-Mar-01 15:26
    memberJim Koornneef1-Mar-01 15:26 
    GeneralMy wislist item for this cool class... Pin
    Jason Hattingh20-Nov-00 3:20
    memberJason Hattingh20-Nov-00 3:20 
    GeneralRetrieving the command ID(s) Pin
    Dan Scott8-Sep-00 6:00
    sussDan Scott8-Sep-00 6:00 
    GeneralRe: Retrieving the command ID(s) Pin
    Mazhekin7-Sep-04 2:34
    memberMazhekin7-Sep-04 2:34 
    Generalvery very nice !!!, but ... Pin
    guy24-Aug-00 5:13
    sussguy24-Aug-00 5:13 
    GeneralRe: very very nice !!!, but ... Pin
    Zulfiqar Inayat Malik25-Jun-01 0:26
    memberZulfiqar Inayat Malik25-Jun-01 0:26 
    GeneralGood work Pin
    Ken Koornneef9-Aug-00 7:51
    sussKen Koornneef9-Aug-00 7:51 
    GeneralNot working in VC5 Pin
    sten struikenkamp19-Jul-00 5:52
    susssten struikenkamp19-Jul-00 5:52 
    GeneralRe: Not working in VC5 Pin
    Jim Koornneef9-Aug-00 2:57
    sussJim Koornneef9-Aug-00 2:57 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.180318.2 | Last Updated 15 May 2000
    Article Copyright 2000 by Jim Koornneef
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid