Click here to Skip to main content
Click here to Skip to main content

Command Routing for Popup Menus

, 22 Aug 2000 CPOL
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

Comments and Discussions

 
QuestionThanks PinmemberMember 56884439-Feb-15 6:34 
QuestionThanks PinmemberMember 56884439-Feb-15 6:34 
QuestionThis does not solve focus problems? Pinmembersumicus10-Sep-09 3:25 
AnswerRe: This does not solve focus problems? PinmemberNoel Dillabough10-Sep-09 6:12 
GeneralThere is no need for this at all PinmemberMartin Richter [MVP C++]18-Jul-07 21:48 
GeneralRe: There is no need for this at all PinmemberNoel Dillabough19-Jul-07 6:22 
GeneralRe: There is no need for this at all PinmemberMartin Richter [MVP C++]19-Jul-07 6:52 
GeneralRe: There is no need for this at all PinmemberNoel Dillabough19-Jul-07 13:04 
QuestionSmall bug with auto disabling? PinsussJim Kuzeja2-Sep-05 12:13 
GeneralA note on how MFC::ON_UPDATE_COMMAND_UI actually works PinmemberRoger Allen2-Dec-04 5:39 
GeneralSmall Enhancement Pinmemberkam1724-Apr-03 16:18 
GeneralRe: Small Enhancement Pinmemberzjohnr10-May-04 13:24 
GeneralRe: Small Enhancement PinmemberNoel Dillabough26-Aug-05 12:14 
QuestionIs it possible to change the MessageHandler of a SubMenu in the MainWnd with this code? PinmemberAndreas Glaubitz10-Apr-03 22:39 
AnswerRe: Is it possible to change the MessageHandler of a SubMenu in the MainWnd with this code? PinsussAnonymous8-Jul-04 3:43 
GeneralDragDrop menu (like Windows Start Menu) Pinsussmajesty16-Jan-03 6:28 
Generaldisable the menu item from view class Pinsusskishore1129-Dec-02 23:27 
QuestionHow to create menu as style in XP in VB PinsussAnonymous11-Sep-02 6:27 
AnswerRe: How to create menu as style in XP in VB Pinmemberalejorb16-Oct-03 12:22 
QuestionHow to create menu in the parent window with file,edit,view as menu items--immediate response required help Pinmembersuresh sundar2-May-01 22:03 
GeneralVery Nice! PinmemberMark Findlay2-Apr-01 16:15 
GeneralRe: Very Nice! PinmemberCodin' Carlos21-Jan-02 11:42 
QuestionOut of topic... Could anybody help me, please? PinsussMartin Vrbovsky6-Oct-00 3:01 
GeneralThank You !! PinsussRonald L. Russell Jr. (Ron)7-Sep-00 3:48 
QuestionMFC Implementation of this? PinsussCraig Henderson23-Aug-00 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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