Click here to Skip to main content
15,886,199 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.2K   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

 
QuestionThanks Pin
Member 56884439-Feb-15 5:34
Member 56884439-Feb-15 5:34 
QuestionThanks Pin
Member 56884439-Feb-15 5:34
Member 56884439-Feb-15 5:34 
QuestionThis does not solve focus problems? Pin
sumicus10-Sep-09 2:25
sumicus10-Sep-09 2:25 
AnswerRe: This does not solve focus problems? Pin
Noel Dillabough10-Sep-09 5:12
Noel Dillabough10-Sep-09 5:12 
GeneralThere is no need for this at all Pin
Martin Richter [rMVP C++]18-Jul-07 20:48
Martin Richter [rMVP C++]18-Jul-07 20:48 
GeneralRe: There is no need for this at all Pin
Noel Dillabough19-Jul-07 5:22
Noel Dillabough19-Jul-07 5:22 
GeneralRe: There is no need for this at all Pin
Martin Richter [rMVP C++]19-Jul-07 5:52
Martin Richter [rMVP C++]19-Jul-07 5:52 
GeneralRe: There is no need for this at all Pin
Noel Dillabough19-Jul-07 12:04
Noel Dillabough19-Jul-07 12:04 
GeneralRe: There is no need for this at all <- WRONG Pin
Terence Russell29-Apr-16 9:49
Terence Russell29-Apr-16 9:49 
QuestionSmall bug with auto disabling? Pin
jkuzeja2-Sep-05 11:13
jkuzeja2-Sep-05 11:13 
GeneralA note on how MFC::ON_UPDATE_COMMAND_UI actually works Pin
Roger Allen2-Dec-04 4:39
Roger Allen2-Dec-04 4:39 
GeneralSmall Enhancement Pin
kam1724-Apr-03 15:18
kam1724-Apr-03 15:18 
GeneralRe: Small Enhancement Pin
zjohnr10-May-04 12:24
zjohnr10-May-04 12:24 
GeneralRe: Small Enhancement Pin
Noel Dillabough26-Aug-05 11:14
Noel Dillabough26-Aug-05 11:14 
QuestionIs it possible to change the MessageHandler of a SubMenu in the MainWnd with this code? Pin
Andreas Glaubitz10-Apr-03 21:39
Andreas Glaubitz10-Apr-03 21:39 
AnswerRe: Is it possible to change the MessageHandler of a SubMenu in the MainWnd with this code? Pin
Anonymous8-Jul-04 2:43
Anonymous8-Jul-04 2:43 
GeneralDragDrop menu (like Windows Start Menu) Pin
Pavel Sommer16-Jan-03 5:28
Pavel Sommer16-Jan-03 5:28 
Generaldisable the menu item from view class Pin
mechanical102129-Dec-02 22:27
mechanical102129-Dec-02 22:27 
QuestionHow to create menu as style in XP in VB Pin
Anonymous11-Sep-02 5:27
Anonymous11-Sep-02 5:27 
AnswerRe: How to create menu as style in XP in VB Pin
alejorb16-Oct-03 11:22
alejorb16-Oct-03 11:22 
visited following site:
OMG | :OMG: http://www.vbaccelerator.com/home/VB/Code/Controls/Menus/index.asp
bye
QuestionHow to create menu in the parent window with file,edit,view as menu items--immediate response required help Pin
2-May-01 21:03
suss2-May-01 21:03 
GeneralVery Nice! Pin
Mark Findlay2-Apr-01 15:15
Mark Findlay2-Apr-01 15:15 
GeneralRe: Very Nice! Pin
Codin' Carlos21-Jan-02 10:42
Codin' Carlos21-Jan-02 10:42 
QuestionOut of topic... Could anybody help me, please? Pin
Martin Vrbovsky6-Oct-00 2:01
sussMartin Vrbovsky6-Oct-00 2:01 
GeneralThank You !! Pin
Ronald L. Russell Jr. (Ron)7-Sep-00 2:48
sussRonald L. Russell Jr. (Ron)7-Sep-00 2:48 

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.