
Updating menu items in dialog-based apps: Solution
This is sorta an answer to my own posted questions on menus in dialog
boxes.
The issue is how to use functions created/mapped by Class Wizard to
ON_COMMAND_UI messages in a dialog-based app. Normally, they don't
wrok, as they're usually handled by CFrameWnd, which a dialog app
gave a clue, and this is my formulation of it. It's just a re-hash of
what Kenneth posted, but formulated into nice files you can add to
future project easily. Hopes this helps some one else, and thanks to
Kenneth for the pointer.
- Randy Langer
--------------------------------------------------------------------
/* DialogMenuUpdate.h */
#if !defined(DIALOGMENUUPDATE_H__)
#define DIALOGMENUUPDATE_H__
/********************************************************************
This function is used to use the ON_COMMAND_UI command mappings in
dialog-based apps. Normally these won't work for a dialog app, since
they're normally hannled by CFrameWnd, which a dialog-based app lacks.
You can, however, use these Class Wizard created mappings (which shows
up in the CWinApp based class, not the dialog class), by the following
method:
1. Use Class Wizard to map ON_COMMAND_UI for menu items in the usual
fashion. Note that they're mapped to the CWinApp-based class, not the
dialog class.
2. In the dialog class, add ON_WM_INITMENUPOPUP() to the message map
list, *OUTSIDE* of the "{{AFX_MSG_MAP(CVersDlg)" block (sorry, Class
Wizard won't do this for you).
3. Add the following function to the dialog header file:
void OnInitMenuPopup(CMenu *, UINT, BOOL);
4. Add the following function to the dialog implementation (.cpp)
file:
void CYourDlg::OnInitMenuPopup(CMenu *a, UINT b, BOOL c)
{
DialogMenuUpdate(this, a, b, c);
CDialog::OnInitMenuPopup(a, b, c);
}
(using the correct class name for 'CYourDlg', of course). Also,
#include this header file you're reading to the dialog class
implementation file.
5. Add 'DialogMenuUpdate.cpp' to your project.
********************************************************************/
void DialogMenuUpdate(CWnd *, CMenu *, UINT, BOOL);
#endif
--------------------------------------------------------------------
/* DialogMenuUpdate.cpp */
#include "stdafx.h"
#include "DialogMenuUpdate.h"
void
DialogMenuUpdate(CWnd *p, CMenu *y, UINT, BOOL sysCmd)
{
if(sysCmd)
return;
CCmdUI q;
ASSERT(y != NULL);
q.m_pMenu = y;
ASSERT(q.m_pOther == NULL);
q.m_nIndexMax = y->GetMenuItemCount();
for(q.m_nIndex = 0; q.m_nIndex < q.m_nIndexMax; q.m_nIndex++)
{
if(q.m_nID = y->GetMenuItemID(q.m_nIndex))
{
ASSERT(q.m_pOther == NULL);
ASSERT(q.m_pMenu != NULL);
if(q.m_nID == (UINT) -1)
{
q.m_pSubMenu = y->GetSubMenu(q.m_nIndex);
if(q.m_pSubMenu &&
(q.m_nID = q.m_pSubMenu->GetMenuItemID(0)) &&
q.m_nID != (UINT) -1)
q.DoUpdate(p, FALSE);
}
else
{
q.m_pSubMenu = NULL;
q.DoUpdate(p, q.m_nID < 0xF000);
}
}
}
Quote:
}
--- End of post ---------------------------------------------------