Click here to Skip to main content
15,568,300 members
Articles / Desktop Programming / MFC
Article
Posted 7 Feb 2002

Stats

1.2M views
27.7K downloads
242 bookmarked

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
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: Painting bug with submenus Pin
    xtmono7-Oct-04 0:29
    xtmono7-Oct-04 0:29 
    GeneralRe: Painting bug with submenus Pin
    fjlaga7-Oct-04 9:10
    fjlaga7-Oct-04 9:10 
    GeneralRe: Painting bug with submenus Pin
    xtmono14-Oct-04 22:34
    xtmono14-Oct-04 22:34 
    GeneralRe: Painting bug with submenus Pin
    fjlaga15-Oct-04 6:29
    fjlaga15-Oct-04 6:29 
    GeneralGreate! Re: Painting bug with submenus Pin
    xuell001-Dec-05 21:47
    xuell001-Dec-05 21:47 
    GeneralProblem With CComboBoxXP Pin
    suvi_rahul18-Mar-04 0:08
    suvi_rahul18-Mar-04 0:08 
    GeneralSystem Menu Pin
    Chen Hao5-Mar-04 18:26
    Chen Hao5-Mar-04 18:26 
    GeneralPainting bug with very long popup menus Pin
    fjlaga5-Mar-04 10:12
    fjlaga5-Mar-04 10:12 
    I've been using CMenuXP for some time now in my app and it has been great! However, I have noticed a bug. In some cases in my app a very long popup menu is opened with TrackPopupMenu. This popup menu contains so many items that all of them cannot be displayed on screen. To accomodate, the menu puts an up arrow at the top of the menu and a down arrow at the bottom. These arrows allow the user to scroll up/down thru the menu items. Something like this:

    +-----------------+
    | /\ |
    | Menu Item 1 |
    | Menu Item 2 |
    | Menu Item 3 |
    | . |
    | . |
    | . |
    | Menu Item N |
    | \/ |
    +-----------------+

    The bug is that the entire rect containing the up/down arrow does not paint -- you can see the app/desktop behind showing thru. When you move the mouse over the location where the up/down arrow is supposed to be, the arrow shows up. The rest of the rect never paints.

    The area for the up/down area does not come thru as a menu item. How do you draw this part of the menu? I've tried some things in OnNcPaint, but it doesn't seem like the right place to do it. Any ideas?

    -Frank

    GeneralRe: Painting bug with very long popup menus Pin
    fjlaga18-Mar-04 11:06
    fjlaga18-Mar-04 11:06 
    GeneralPopups and dialog controls Pin
    Tomas Urban5-Mar-04 7:15
    Tomas Urban5-Mar-04 7:15 
    GeneralRe: Popups and dialog controls Pin
    Tomas Urban5-Mar-04 22:56
    Tomas Urban5-Mar-04 22:56 
    GeneralBitmaps Pin
    Daniel_h5-Feb-04 4:47
    Daniel_h5-Feb-04 4:47 
    GeneralIn WinXP, slight toolbar issue Pin
    vmelkon29-Jan-04 8:17
    vmelkon29-Jan-04 8:17 
    QuestionXP 2003 style ? Pin
    FPF28-Jan-04 3:05
    FPF28-Jan-04 3:05 
    Generalautor of CMenuXP wanted live or dead Pin
    Member 3813134-Jan-04 23:41
    Member 3813134-Jan-04 23:41 
    GeneralRe: autor of CMenuXP wanted live or dead Pin
    Jean-Michel LE FOL4-Jan-04 23:52
    Jean-Michel LE FOL4-Jan-04 23:52 
    Generalthx God ur still alive :) Pin
    Anonymous20-Jan-04 22:17
    Anonymous20-Jan-04 22:17 
    GeneralPopUP Menu Pin
    suvi_rahul11-Dec-03 23:52
    suvi_rahul11-Dec-03 23:52 
    GeneralRe: PopUP Menu Pin
    FPF28-Jan-04 2:59
    FPF28-Jan-04 2:59 
    GeneralRe: PopUP Menu Pin
    suvi_rahul16-Mar-04 23:51
    suvi_rahul16-Mar-04 23:51 
    GeneralRe: PopUP Menu Pin
    Jean-Michel LE FOL17-Mar-04 6:04
    Jean-Michel LE FOL17-Mar-04 6:04 
    GeneralRe: PopUP Menu Pin
    suvi_rahul17-Mar-04 23:35
    suvi_rahul17-Mar-04 23:35 
    GeneralRe: PopUP Menu Pin
    Jean-Michel LE FOL18-Mar-04 22:27
    Jean-Michel LE FOL18-Mar-04 22:27 
    QuestionWhere are the triangles drawn for submenus? Pin
    tom_olinger10-Dec-03 18:18
    tom_olinger10-Dec-03 18:18 
    AnswerRe: Where are the triangles drawn for submenus? Pin
    fjlaga_old18-Mar-04 11:12
    fjlaga_old18-Mar-04 11:12 

    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.