Click here to Skip to main content
Licence CPOL
First Posted 22 Aug 2000
Views 150,889
Bookmarked 59 times

Command Routing for Popup Menus

By | 22 Aug 2000 | Article
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)

About the Author

Noel Dillabough

Software Developer (Senior)

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThis does not solve focus problems? Pinmembersumicus2:25 10 Sep '09  
AnswerRe: This does not solve focus problems? PinmemberNoel Dillabough5:12 10 Sep '09  
GeneralThere is no need for this at all PinmemberMartin Richter [MVP C++]20:48 18 Jul '07  
GeneralRe: There is no need for this at all PinmemberNoel Dillabough5:22 19 Jul '07  
GeneralRe: There is no need for this at all PinmemberMartin Richter [MVP C++]5:52 19 Jul '07  
GeneralRe: There is no need for this at all PinmemberNoel Dillabough12:04 19 Jul '07  
QuestionSmall bug with auto disabling? PinsussJim Kuzeja11:13 2 Sep '05  
GeneralA note on how MFC::ON_UPDATE_COMMAND_UI actually works PinPopularmemberRoger Allen4:39 2 Dec '04  
GeneralSmall Enhancement Pinmemberkam1715:18 24 Apr '03  
GeneralRe: Small Enhancement Pinmemberzjohnr12:24 10 May '04  
GeneralRe: Small Enhancement PinmemberNoel Dillabough11:14 26 Aug '05  
QuestionIs it possible to change the MessageHandler of a SubMenu in the MainWnd with this code? PinmemberAndreas Glaubitz21:39 10 Apr '03  
AnswerRe: Is it possible to change the MessageHandler of a SubMenu in the MainWnd with this code? PinsussAnonymous2:43 8 Jul '04  
GeneralDragDrop menu (like Windows Start Menu) Pinsussmajesty5:28 16 Jan '03  
Generaldisable the menu item from view class Pinsusskishore1122:27 29 Dec '02  
QuestionHow to create menu as style in XP in VB PinsussAnonymous5:27 11 Sep '02  
AnswerRe: How to create menu as style in XP in VB Pinmemberalejorb11:22 16 Oct '03  
QuestionHow to create menu in the parent window with file,edit,view as menu items--immediate response required help Pinmembersuresh sundar21:03 2 May '01  
GeneralVery Nice! PinmemberMark Findlay15:15 2 Apr '01  
GeneralRe: Very Nice! PinmemberCodin' Carlos10:42 21 Jan '02  
QuestionOut of topic... Could anybody help me, please? PinsussMartin Vrbovsky2:01 6 Oct '00  
GeneralThank You !! PinsussRonald L. Russell Jr. (Ron)2:48 7 Sep '00  
QuestionMFC Implementation of this? PinsussCraig Henderson3:24 23 Aug '00  
AnswerRe: MFC Implementation of this? PinsussNoel6:39 23 Aug '00  
AnswerRe: MFC Implementation of this? PinsussNoel9:05 23 Aug '00  

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.

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