Why are my pop-up menus
disabled in dialogs?
MFC Tutorial: WM_INITMENUPOPUP,WM_INITMENUPOPUP,WM_INITMENUPOPUP,
menu in dialog
Basically, when a menu is
about to pop up, a WM_INITMENUPOP
message is sent to the parent window. In the base class of
the CMainFrame, there is already a handler for the WM_INITMENUPOPUP,
and it sends the UPDATE_COMMAND_UI
sequence to the mainframe, the view, and the App classes,
but not to the dialog.
So, you have two options:
1. Have your view class for all the menu enabling for the
dialog. I'm not crazy about this approach.
2. Have your view class pass of it's processing to the
current dialog, if one exists. I like this.
So, in order to
accomplish #2, let's first take a look at how your doing
your dialog now:
In order to accomplish #2
above, we need to have the OnCmdMsg()
function (added via classwizard) for your view call the OnCmdMsg
for your dialog. However, the OnCmdMsg
function can't see the X object in OnButton1, because it's
a local variable.
So, you would do the
Create a member variable in your view, called pDialog,
thats a CDialog *:
In your view constructor,
set the pointer to 0.
pDialog = 0;
Change your OnButton1, so
that it initializes the dialog pointer:
pDialog = new CSomeDlg;
pDialog = 0;
Finally, change your view
so that it's OnCmdMsg look like the following (you may
need to add OnCmdMsg):
nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO*
if( pDialog && pDialog->OnCmdMsg(
nID, nCode, pExtra, pHandlerInfo ) )
return( TRUE );
nCode, pExtra, pHandlerInfo);
In addition to the above,
in your dialog class you need to *manually* add the ON_UPDATE_COMMAND_UI
and ON_COMMAND macros for the routines that will be
responsible for enabling/disabling a menu item, and
specifying the function to call when the menu item is