Andrew Truckle
Andrew Truckle

Reputation: 19207

Using a menu button with CMFCToolBar in CDialog/CDialogEx

I have this code:

CMenu menu;
menu.LoadMenu(IDR_MENU_TOOLBAR_REPORT_VIEWER);  // Load your menu resource
CMenu* pSubMenu = menu.GetSubMenu(0);   // Get the first submenu (or the one you need)
CMFCToolBarMenuButton menuButton(ID_TOOLBAR_REPORT_VIEWER_CONGREGATION, pSubMenu->Detach(), 2, L"123");
m_ToolBar.RemoveButton(2);
m_ToolBar.InsertButton(menuButton, 2);
m_ToolBar.AdjustLayout();
m_ToolBar.Invalidate();
m_ToolBar.UpdateWindow();
m_ToolBar.SendMessage(WM_IDLEUPDATECMDUI, (WPARAM)FALSE);

It replaces the toolbar icon with a menu button. I have added menu item handlers too (command / update).

But, when I run it the menu items are all disabled. Why?

I have also tried called in OnInitDialog:

UpdateDialogControls(this, false);

Makes no difference. I understood that calling UpdateDialogControls in this manner would ensure that all controls would get processed.

Related

One thing I can say is that OnInitMenuPopup is triggered when the dialogs menu gets displayed, but it does not get triggered when the tool bar button menu gets displayed.

Also, I have tried making a call to m_ToolBar.SetRouteCommandsViaFrame(false); but again, it makes no difference. OnInitMenuPopup is not being triggered when the menu is displayed. So I have no opportunity to update the menu items states.

I have simplified the code:

CMenu menu;
menu.LoadMenu(IDR_MENU_TOOLBAR_REPORT_VIEWER);  // Load your menu resource

m_ToolBar.ReplaceButton(ID_MANAGE_REPORT_VIEWER,
    CMFCToolBarMenuButton((UINT)-1, menu.GetSubMenu(0)->Detach(), 2, L"", TRUE));

m_ToolBar.AdjustLayout();
m_ToolBar.AdjustSizeImmediate();
m_ToolBar.SetRouteCommandsViaFrame(false);

But, the result is just the same.

Upvotes: 0

Views: 103

Answers (0)

Related Questions