Click here to Skip to main content
15,892,927 members
Articles / Desktop Programming / MFC
Article

Command Routing for Popup Menus

Rate me:
Please Sign up or sign in to vote.
4.79/5 (28 votes)
22 Aug 2000CPOL 194.3K   62   30
Handle grayed/disabled/checked menu items using the familiar OnUpdate interface

Introduction

While working with popup menus, I needed a way to hook these into the standard CCmdUI MFC OnUpdate interface. The following block of code does this:

C++
void CmdRouteMenu(CWnd* pWnd,CMenu* pPopupMenu)
{
    CCmdUI state;
    state.m_pMenu = pPopupMenu;
    state.m_pParentMenu = pPopupMenu;
    state.m_nIndexMax = pPopupMenu->GetMenuItemCount();

    for (state.m_nIndex = 0; 
         state.m_nIndex < state.m_nIndexMax; 
         state.m_nIndex++) 
    {
        state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);

        // menu separator or invalid cmd - ignore it
        if (state.m_nID == 0) continue; 

        if (state.m_nID == (UINT)-1)
        {
            // possibly a popup menu, route to child menu if so
            CMenu* pSub=pPopupMenu->GetSubMenu(state.m_nIndex);
            if(pSub) CmdRouteMenu(pWnd,pSub);
        }
        else 
        {
            // normal menu item, Auto disable if command is 
            // _not_ a system command.
            state.m_pSubMenu = NULL;
            state.DoUpdate(pWnd, FALSE);
        }
    }
}

Usage Example:

CmdRouteMenu(pWnd,pSubMenu);
pSubMenu->TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON | 
                         TPM_LEFTALIGN,point.x,point.y,pWnd,NULL);

Simply call with the window to handle the OnUpdate messages (usually your MainWnd) and the menu to be worked on, just before popping up the menu using TrackMenu(...). This also works for BMP menu and Gradient menu also found on CodeProject.

Of course, the code above only works if you have the OnUpdate calls written to handle the menu commands.

License

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


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMFC Implementation of this? Pin
Craig Henderson23-Aug-00 3:24
Craig Henderson23-Aug-00 3:24 
AnswerRe: MFC Implementation of this? Pin
Noel Dillabough23-Aug-00 6:39
Noel Dillabough23-Aug-00 6:39 
AnswerRe: MFC Implementation of this? Pin
Noel Dillabough23-Aug-00 9:05
Noel Dillabough23-Aug-00 9:05 
AnswerRe: MFC Implementation of this? Pin
Jason De Arte16-Dec-00 10:49
Jason De Arte16-Dec-00 10:49 
AnswerRe: MFC Implementation of this? Pin
25-Feb-02 9:19
suss25-Feb-02 9:19 

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.