Click here to Skip to main content
13,358,478 members (55,068 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 2 Nov 2000

Issues with popup menu owned by a non CFrameWnd derived class

, 2 Nov 2000
Rate this:
Please Sign up or sign in to vote.
How to handle UpdateCommandUI messages for menus not owned by CFrameWnd windows


Have you had problems with having a popup menu in a window and not being able to get its UpdateCommandUI messages?

The simplest solution is (if it doesn’t create any kind of impropriety) making the popup menu owned by CFrameWnd derived class. You may want to make a non CFrameWnd derived class own the menu though. This becomes a requirement when you have a dll, which you want to provide as a plug-in. You cannot have the mainframe class of the target application which links to the dll implement the message service routines for your popup menu! Even if it were ok, it would be pretty bad to have mainframe class have all popup menu service code as far as even modularity is concerned.


Add ON_WM_INITMENUPOPUP()to the message map in the class which you assign as the owner in TrackPopUpMenu(). This handles the WM_INITMENUPOPUPwindow messsage sent before a popup appears. TrackPopupMenu() would send this message before the menu is shown. Here is where we have to generate the UpdateCommandUI for the menu items.

void CPvMyWnd::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
    CFrameWnd* pwndFrame = AfxGetMainWnd();

    if(NULL != pwndFrame)

This will let you have a pointer to a window of some type say CMyWnd own the menu and still have update command UI messages sent for each of the menu item ids. I would sincerely recommend you to take a look at the CFrameWnd::OnInitPopUpMenu() function to get a full picture as to how it generates an Update Command UI message for popup menus.

Now one more issue you may face with a popup menu having a non CFrameWnd derived class as the parent. When you initiate a new UI like a dialog creation when the menu is still dropped down, it locks up the application because of the popup not being closed, as in the case with combo box drop downs. CFrameWnd takes care of these and this time you will have to do it yourself. The following line of code will help you there if you send a message to the owner window of the popup, before you want to create any other UI and it have the focus.


A typical situation is a client server application where you get a server message requiring a UI creation to convey some information to the user and this can happen when the user was having fun with his popup menu and the application appears to locked up till you minimize and restore causing popup state change. I think I wouldn’t be pleased with such a treatment, so the fix. Now the combo box - he also shows some personality traits to pick a fight whenever someone new tries to cross roads while he has a dropdown. Simply use the function

void AFXAPI AfxCancelModes(HWND hWndRcvr) // declared in afximpl.h

look in winutil.cpp in mfc source. The handle to the window that may have a child combobox with a dropped down state represents hWndRcvr.

Going one step further How to send Update Command UI messages to any control in a given window..

CmdUI.m_pOther = CMyButtonWndClass;

This will send an UpdateCommandUI message to IDC_MYBUTTON to this class. In general the this pointer is a pointer to a CDialog or CFormView object.

Well how did I know? I read CFrameWnd::OnInitPopUpMenu().


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Cheeran Santhosh
Software Developer (Senior)
United States United States
I am a software programmer with 17 years of experience working on Microsoft technologies using various tools. Using spare time I play Ping Pong and enjoy music and movies.I come from Trichur, a town in kerala, India. I love code forums and feel obligated to contribute every once in a while.

I think Internet and the information share it provides is mind boggling. Do we ever believe, tomorrow always brings to reality,above and beyond what dreams can glimpse.

You may also be interested in...

Comments and Discussions

GeneralXP Visual Styled PopUp Menu Pin
Anil K P2-Jul-07 19:09
memberAnil K P2-Jul-07 19:09 
GeneralDisplay menu on CWnd Pin
Linus Zheng30-Jun-04 17:35
memberLinus Zheng30-Jun-04 17:35 
GeneralRe: Display menu on CWnd Pin
Cheeran Santhosh1-Jul-04 15:29
memberCheeran Santhosh1-Jul-04 15:29 
GeneralRe: Display menu on CWnd Pin
ChangMunJun17-Mar-05 5:41
memberChangMunJun17-Mar-05 5:41 
GeneralGet handle of popupmenu Pin
naveensg8-Jul-03 3:05
membernaveensg8-Jul-03 3:05 
GeneralDisabling Menu Items Pin
ThomasBoseKenn27-Mar-03 18:55
memberThomasBoseKenn27-Mar-03 18:55 
QuestionCan i display a popup menu on CDialog Pin
Anonymous26-Sep-02 22:07
sussAnonymous26-Sep-02 22:07 
GeneralCorrection to the article Pin
cheeran20-Aug-02 12:24
susscheeran20-Aug-02 12:24 
GeneralTrackPopupMenu Pin
Ron Hawkins14-Nov-01 9:14
memberRon Hawkins14-Nov-01 9:14 
QuestionAnd if I don't want it to be sent to CMainFrame? Pin
Andreas Thorsén14-Jun-01 2:41
memberAndreas Thorsén14-Jun-01 2:41 
AnswerRe: And if I don't want it to be sent to CMainFrame? Pin
Codin' Carlos21-Jan-02 11:39
memberCodin' Carlos21-Jan-02 11:39 
GeneralThis info should be in msdn! Pin
Andreas Thorsén13-Jun-01 22:27
memberAndreas Thorsén13-Jun-01 22:27 

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
Web01 | 2.8.180111.1 | Last Updated 3 Nov 2000
Article Copyright 2000 by Cheeran Santhosh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid