Click here to Skip to main content
13,002,628 members (68,817 online)
Click here to Skip to main content
Add your own
alternative version

Stats

173.1K views
60 bookmarked
Posted 22 Aug 2000

Command Routing for Popup Menus

, 22 Aug 2000
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Noel Dillabough
Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThanks Pin
Member 56884439-Feb-15 5:34
memberMember 56884439-Feb-15 5:34 
QuestionThanks Pin
Member 56884439-Feb-15 5:34
memberMember 56884439-Feb-15 5:34 
QuestionThis does not solve focus problems? Pin
sumicus10-Sep-09 2:25
membersumicus10-Sep-09 2:25 
AnswerRe: This does not solve focus problems? Pin
Noel Dillabough10-Sep-09 5:12
memberNoel Dillabough10-Sep-09 5:12 
GeneralThere is no need for this at all Pin
Martin Richter [MVP C++]18-Jul-07 20:48
memberMartin Richter [MVP C++]18-Jul-07 20:48 
GeneralRe: There is no need for this at all Pin
Noel Dillabough19-Jul-07 5:22
memberNoel Dillabough19-Jul-07 5:22 
GeneralRe: There is no need for this at all Pin
Martin Richter [MVP C++]19-Jul-07 5:52
memberMartin Richter [MVP C++]19-Jul-07 5:52 
GeneralRe: There is no need for this at all Pin
Noel Dillabough19-Jul-07 12:04
memberNoel Dillabough19-Jul-07 12:04 
GeneralRe: There is no need for this at all <- WRONG Pin
Terence Russell29-Apr-16 9:49
memberTerence Russell29-Apr-16 9:49 
QuestionSmall bug with auto disabling? Pin
Jim Kuzeja2-Sep-05 11:13
sussJim Kuzeja2-Sep-05 11:13 
GeneralA note on how MFC::ON_UPDATE_COMMAND_UI actually works Pin
Roger Allen2-Dec-04 4:39
memberRoger Allen2-Dec-04 4:39 
GeneralSmall Enhancement Pin
kam1724-Apr-03 15:18
memberkam1724-Apr-03 15:18 
GeneralRe: Small Enhancement Pin
zjohnr10-May-04 12:24
memberzjohnr10-May-04 12:24 
GeneralRe: Small Enhancement Pin
Noel Dillabough26-Aug-05 11:14
memberNoel 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
memberAndreas 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
sussAnonymous8-Jul-04 2:43 
GeneralDragDrop menu (like Windows Start Menu) Pin
majesty16-Jan-03 5:28
sussmajesty16-Jan-03 5:28 
Generaldisable the menu item from view class Pin
kishore1129-Dec-02 22:27
susskishore1129-Dec-02 22:27 
QuestionHow to create menu as style in XP in VB Pin
Anonymous11-Sep-02 5:27
sussAnonymous11-Sep-02 5:27 
AnswerRe: How to create menu as style in XP in VB Pin
alejorb16-Oct-03 11:22
memberalejorb16-Oct-03 11:22 
QuestionHow to create menu in the parent window with file,edit,view as menu items--immediate response required help Pin
suresh sundar2-May-01 21:03
membersuresh sundar2-May-01 21:03 
GeneralVery Nice! Pin
Mark Findlay2-Apr-01 15:15
memberMark Findlay2-Apr-01 15:15 
GeneralRe: Very Nice! Pin
Codin' Carlos21-Jan-02 10:42
memberCodin' 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170626.1 | Last Updated 23 Aug 2000
Article Copyright 2000 by Noel Dillabough
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid