VC++
MFC Tutorial: WM_INITMENUPOPUP,WM_INITMENUPOPUP,WM_INITMENUPOPUP,
menu in dialog
|
|
Q.
Why are my pop-up menus
disabled in dialogs?
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:
void CWhateverView::OnButton1()
{
CSomeDlg X;
X.DoModal();
}
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
following:
Create a member variable in your view, called pDialog,
thats a CDialog *:
CDialog* pDialog;
In your view constructor,
set the pointer to 0.
pDialog = 0;
Change your OnButton1, so
that it initializes the dialog pointer:
void CWhateverView::OnButton1()
{
pDialog = new CSomeDlg;
pDialog->DoModal();
delete pDialog;
pDialog = 0;
}
Finally, change your view
so that it's OnCmdMsg look like the following (you may
need to add OnCmdMsg):
BOOL CWhateverView::OnCmdMsg(UINT
nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO*
pHandlerInfo)
{
if( pDialog && pDialog->OnCmdMsg(
nID, nCode, pExtra, pHandlerInfo ) )
return( TRUE );
return CFormView::OnCmdMsg(nID,
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
invoked.
|