Click here to Skip to main content
15,900,378 members
Articles / Desktop Programming / MFC
Article

CMenuXP - The Office XP Style Menu

Rate me:
Please Sign up or sign in to vote.
4.94/5 (114 votes)
13 Jul 2003CPOL2 min read 1.3M   27.9K   242   342
Owner drawn menu with the Office XP visual style

Sample Image - MenuXP.gif

Menus in dialog windows : Sample Image (Menus in Dialog Window)- MenuXP_dlg.gif

Introduction

This article presents an implementation of an owner drawn menu with the Office XP and Visual Studio .NET visual style.

Using the code

To use the CMenuXP class in your projects, you have to add 3 macros in your code:

  1. In the header file of the class that handles the menu (probably the MainFrame):
    #include "Tools/MenuXP.h"    // Before the declaration of the class
    // ...
    
    DECLARE_MENUXP()             // Into the definition of the class
  2. In the source file of the same class:
    IMPLEMENT_MENUXP(className, baseClass);
  3. In the message map of the class:
    BEGIN_MESSAGE_MAP(className, baseClass)
        // ...
        ON_MENUXP_MESSAGES()   // <-- Added line
    END_MESSAGE_MAP()

To make borders flat, I subclass the popup menu window managed by the system. To make it possible, you have to add those 2 calls in your code:

  1. In the InitInstance method of your CWinApp derived class:
    CMenuXP::InitializeHook();
  2. In the ExitInstance method of your CWinApp derived class:
    CMenuXP::UninitializeHook();

To make the menubar flat, you must do this call:

CMenuXP::UpdateMenuBar (pFrameWnd); // pFrameWnd refers to the frame that 
                                    // contains the MenuBar

The best place for this call depends of the frame type:

  • For MDI application, into the OnUpdateFrameMenu method of the child windows.
  • For SDI application, into the LoadFrame method of the main frame.

Finally, don't forget the last call:

CMenuXP::SetXPLookNFeel (pFrameWnd); // refers to the frame that contains 
                                     // the MenuBar

Without this call, menus will appear in a standard mode.

    To draw the menu items, I reused some classes already presented in my article: Office XP look & feel.

    History

    • 02/08/2002 - First release.
    • 01/06/2003 - Adding flat borders and flat menubar.
    • 05/31/2003 - Kris Wojtas has updated the source code to allow MenuXP to draw "radio", "check" state and gradient under bitmaps.
    • 06/02/2003 - Corrections, radio items support (thanks to Kris Wojtas) and new demos (flat controls and dialog based sample). You can get the latest update to this article at www.azsoft.free.fr.
    • 14 Jul 03 - further update by Kris:

      Sample Image

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Web Developer CSC
    France France
    Jean-Michel LE FOL is a GraphTalk product architect.
    GraphTalk is a set of products which cover the whole scope of the development process. GraphTalk is used by the main insurance compagnies over the world.
    The development team is currently based in France near Paris.

    Comments and Discussions

     
    GeneralRe: Question Pin
    viron7526-Feb-03 13:02
    viron7526-Feb-03 13:02 
    QuestionHow to use it in VB.Net Pin
    vaidy19-Feb-03 3:26
    vaidy19-Feb-03 3:26 
    GeneralProgram fails with ON_MENUXP_MESSAGES() in CMainFrame Pin
    darthmaul16-Feb-03 21:56
    darthmaul16-Feb-03 21:56 
    GeneralRe: corrected ! Pin
    darthmaul17-Feb-03 23:03
    darthmaul17-Feb-03 23:03 
    QuestionWhat about toolbar ? Pin
    darthmaul12-Feb-03 2:45
    darthmaul12-Feb-03 2:45 
    GeneralSkipped one step Pin
    Alexander Shevchenko7-Feb-03 5:20
    Alexander Shevchenko7-Feb-03 5:20 
    GeneralRe: Skipped one step Pin
    Jean-Michel LE FOL7-Feb-03 9:17
    Jean-Michel LE FOL7-Feb-03 9:17 
    GeneralTrue Colors in CMenuXP Pin
    Izidor31-Jan-03 7:41
    Izidor31-Jan-03 7:41 
    Congratulations, very good and useful article!

    I suggest a little improvement regarding the usage of toolbars with more colors than by defalt.
    I am using true colors toolbars. To get this functionality three bitmaps are to be provided - normal, disabled and hot version of toolbar images.
    Since CMenuXP gets its images from the standard toolbar bitmap it can not take advantage of these true color bitmaps. However, with few simple changes this functionality can be achieved and still retain the normal behavior with simple toolbars.

    Changes in MenuXP.h:
    Change class CImgDesc into:
    class CImgDesc
    {
    public:
        HIMAGELIST m_hImgList;		// NORMAL
        HIMAGELIST m_hImgHotList;	// HOT
        HIMAGELIST m_hImgDisList;   // DISABLED
        int        m_nIndex;
    
        CImgDesc (HIMAGELIST hImgList = NULL, HIMAGELIST hImgHotList = NULL, HIMAGELIST hImgDisList = NULL, int nIndex = 0)
            : m_hImgList (hImgList), m_hImgHotList (hImgHotList), m_hImgDisList (hImgDisList), m_nIndex (nIndex)
        {
        }
    };


    Changes in MenuXP.cpp:
    Change drawing code in CMenuItem::Draw() to:
    if ( GetDisabled() || (bSelected && !GetChecked()) )
    {
    	if(m_ImgDesc.m_hImgDisList && GetDisabled())
    		::ImageList_Draw (m_ImgDesc.m_hImgDisList, m_ImgDesc.m_nIndex, pDC->m_hDC,
                            pRect->left+3, rc.top+3, ILD_TRANSPARENT);
    	else
    	{
    		HICON hIcon = ImageList_ExtractIcon (NULL, m_ImgDesc.m_hImgList, m_ImgDesc.m_nIndex);
    		pDC->DrawState (CPoint (pRect->left + ( bOver ? 4 : 3 ), rc.top + ( bOver ? 4 : 3 )),
    						CSize (IMGWIDTH, IMGHEIGHT), hIcon, DSS_MONO,
    						CBrush (bOver ? HLS_TRANSFORM (::GetSysColor (COLOR_HIGHLIGHT), +50, -66) : HLS_TRANSFORM (::GetSysColor (COLOR_3DFACE), -27, 0)));
    		DestroyIcon (hIcon);
    	}
    }
    if ( !GetDisabled() )
    {
    	if(m_ImgDesc.m_hImgHotList && bSelected)
    		::ImageList_Draw (m_ImgDesc.m_hImgHotList, m_ImgDesc.m_nIndex, pDC->m_hDC,
                            pRect->left+(!GetChecked()? 2 : 3 ), rc.top+(!GetChecked()? 2 : 3 ), ILD_TRANSPARENT);
    	else
    		::ImageList_Draw (m_ImgDesc.m_hImgList, m_ImgDesc.m_nIndex, pDC->m_hDC,
                            pRect->left+( (bSelected && !GetChecked()) ? 2 : 3 ), rc.top+( (bSelected && !GetChecked()) ? 2 : 3 ), ILD_TRANSPARENT);
    }


    Change reading of the images from the toolbar in CMenuXP::SetXPLookNFeel()
    CImgDesc imgDesc ((HIMAGELIST)pBar->SendMessage (TB_GETIMAGELIST, 0, 0),
    					(HIMAGELIST)pBar->SendMessage (TB_GETHOTIMAGELIST, 0, 0),
    					(HIMAGELIST)pBar->SendMessage (TB_GETDISABLEDIMAGELIST, 0, 0),tbbi.iImage);

    AnswerRe: True Colors in CMenuXP Pin
    Frank Isensee10-Oct-05 1:01
    Frank Isensee10-Oct-05 1:01 
    QuestionWin98 Bug? Pin
    Anonymous23-Jan-03 13:55
    Anonymous23-Jan-03 13:55 
    AnswerRe: Win98 Bug? Pin
    Jean-Michel LE FOL23-Jan-03 21:13
    Jean-Michel LE FOL23-Jan-03 21:13 
    GeneralI like it~~ Thanks a lot Pin
    fwsland18-Jan-03 15:30
    fwsland18-Jan-03 15:30 
    GeneralStrange behavior demo-programs, not CMenuXP. Pin
    Alexander Shevchenko17-Jan-03 5:45
    Alexander Shevchenko17-Jan-03 5:45 
    GeneralExcellent Work! Pin
    Chopper16-Jan-03 5:15
    Chopper16-Jan-03 5:15 
    GeneralLittle Bug Pin
    MaTrIX2k215-Jan-03 16:52
    MaTrIX2k215-Jan-03 16:52 
    GeneralRe: Little Bug Pin
    Jean-Michel LE FOL15-Jan-03 22:29
    Jean-Michel LE FOL15-Jan-03 22:29 
    GeneralExcellent work! Pin
    TomPeakz15-Jan-03 14:58
    TomPeakz15-Jan-03 14:58 
    GeneralNice Pin
    kjessee1-Jan-03 15:54
    kjessee1-Jan-03 15:54 
    QuestionHow Can i change the color Pin
    Aymen++29-Nov-02 8:11
    Aymen++29-Nov-02 8:11 
    GeneralNote about deep popups Pin
    moodboom12-Nov-02 7:56
    moodboom12-Nov-02 7:56 
    Generalwow (hold on, picking jaw up off floor...) Pin
    moodboom5-Nov-02 16:22
    moodboom5-Nov-02 16:22 
    Generalanother question Pin
    Kevin Smith5-Nov-02 11:10
    Kevin Smith5-Nov-02 11:10 
    GeneralRe: another question Pin
    Jean-Michel LE FOL15-Jan-03 22:35
    Jean-Michel LE FOL15-Jan-03 22:35 
    Generaldisable menu item Pin
    Kevin Smith2-Nov-02 10:07
    Kevin Smith2-Nov-02 10:07 
    GeneralRe: disable menu item Pin
    moodboom12-Nov-02 6:27
    moodboom12-Nov-02 6:27 

    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.