Dave's Brain

Browse - Programming Tips - How do I change a menu from the CMainFrame class of my MDI application?

Date: 2010apr6
Language: C/C++
Platform: Visual C++

Q.  How do I change a menu from the CMainFrame class of my MDI application?

A.  Your generated MFC MDI application has two menus:
IDR_MAINFRAME and IDR_MEDIATTYPE.  If you do GetMenu() to get a handle
in your CMainFrame's OnCreate() member your changes will disappear
with the menu is changed to IDR_MEDIATTYPE.  The solution is to use
ON_UPDATE_COMMAND_UI() like this.

In MainFrm.h:

afx_msg void OnUpdateSomething(CCmdUI* pCmdUI);
In MainFrm.cpp:
ON_COMMAND(ID_SOMETHING, OnUpdateSomething) ... void CMainFrame::OnUpdateSomething(CCmdUI* pCmdUI) { if (pCmdUI == NULL) return; ... pCmdUI->SetText("<stuff>"); pCmdUI->Enable(<boolean>); ... }
This is great to modify an individual menu item but if you want to add / delete items it doesn't really work.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.