menumfccdialogpopupmenubuttoncmfcmenubutton

CMFCMenuButton does not close


I have a CDialog with a CFMCMenuButton. when I open it, everything appears normally:

CDialog with menu button

The problem happens when I click somewhere else to close the menu. The menu seems unclosable, unless user selects an option of it. And if I press Escape on the keyboard, the dialog closes itself, which is too drastic; I really would want it to close only the menu.

I know I could solve this by setting its property "OS Menu" in the resource file to True (or equivalently, assigning its property m_bOSMenu= TRUE). And I would get:

CDialog with menu button having OS Menu property set to TRUE

As you can see in the image, the consequences are :

How can I solve the problem?


Solution

  • The answer comes from straight from the MFC source.

    The file afxmenubutton.cpp in its CMFCMenuButton::OnShowMenu function advises to use a CDialogEx instead of a CDialog:

    #ifdef _DEBUG
        if ((pParent->IsKindOf(RUNTIME_CLASS(CDialog))) && (!pParent->IsKindOf(RUNTIME_CLASS(CDialogEx))))
        {
            TRACE(_T("CMFCMenuButton parent is CDialog, should be CDialogEx for popup menu handling to work correctly.\n"));
        }
    #endif
    

    Then I changed my dialog class to derive from CDialogEx, and now everything works perfectly! Having the OS Menu property as False, icons are shown and the disabled options are really disabled and grayed out!

    UPDATE: Some documentation on https://msdn.microsoft.com/en-us/library/Bb983913.aspx