1

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 :

  • I loose the icons (not very important)
  • I loose some items' disablings (important) done by OnInitMenuPopup (side note: the second menucolumn is also done by OnInitMenuPopup and it appears as it should be). If user clicks in these options, they will work and they SHOULD NOT.

How can I solve the problem?

MrTux
  • 32,350
  • 30
  • 109
  • 146
sergiol
  • 4,122
  • 4
  • 47
  • 81

1 Answers1

1

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

sergiol
  • 4,122
  • 4
  • 47
  • 81
  • The `TRACE`-output also shows up in Visual Studio's debugger, if attached. Consulting the source code was more trouble than necessary. – IInspectable Aug 14 '15 at 11:12
  • @IInspectable: If you have a very verbose program output, you will not find it in the middle of the text. Or if ou have the program output disabled. – sergiol Aug 14 '15 at 11:15
  • Debug output is searchable. [Ctrl]+F opens a text field to enter search terms. All trace output from MFC classes contains the respective class name. Just in case you want to make your life easier in the future. – IInspectable Aug 14 '15 at 11:23
  • @IInspectable: I already knew that. The point I made is the output you are talking about is not so self-evident in the middle of other text. – sergiol Aug 14 '15 at 11:25