Sorry for the late approval of the answer.
I actually used a workaround to overcome the issue and that was making sure the accelerators for different menus do not collide and let the accel table for the default menu to handle all accelerators. But I had to save menu state and avoid accelerator keys of other menus when the respective menu item was not showing.
Well that was hardly a good workaround. But I got it to work.
Today I tried to validate the answers I received to accept the correct one. The summary is:
I did not need the call to TranslateAccelerator that IInspectable stated.
Loading accelerators the way Vlad suggested does the trick, BUT there is an assertion fail when you call LoadAccelTable as it could be read from the documentation for this function which states that this function should be called only once.
So I overcame it by calling DestroyAcceleratorTable function and setting CFrameWnd::m_hAccelTable to NULL. before calling LoadAccelTable.
Also a sub note for those who want to change menus of SDI or MDI applications the way I did. That also cause a ,don't know why, assertion failed that was overcame by using the method suggested in this article.
To sum it up, in case the provided link becomes unavailable, it says:
- Create a new menu resource (IDR_MYMENU1) in the resource editor.
- Add an HMENU data member to CMyDocument and override GetDefaultMenu() to
return this data member:
// .h file
// HMENU m_hMyMenu;
// virtual HMENU GetDefaultMenu(); // get menu depending on state
HMENU CMyDocument::GetDefaultMenu()
{
return m_hMyMenu; // just use original default
}
Remember to initialize this member variable to NULL either in the
constructor or CDocument::OnNewDocument().
Change and redraw the menu at the desired times. For example, when switching between splitter panes, this is normally done in
CView::OnActivateView(). The following code shows how to implement
it in that function.
// example within CView member function
((CMyDocument*)GetDocument())->m_hMyMenu = ::LoadMenu(
AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MYMENU1));
((CFrameWnd*)AfxGetMainWnd())->OnUpdateFrameMenu(NULL);
AfxGetMainWnd()->DrawMenuBar();
Be sure to destroy the menu upon exiting the application, and avoid
having too many menus loaded at once.
Menus can also be switched simply by changing the value of
CMDIChildWnd::m_hMenuShared (MDI) or CFrameWnd::m_hMenuDefault (SDI).
However, using GetDefaultMenu() allows a safer method of changing the
menus.
Also make sure not to destroy the default menu:
if (m_hMenuDefault != GetMenu()->m_hMenu)
GetMenu()->DestroyMenu();