问题
In a doc/view project using VS2008, a MFCMenuBar seems to load the correct MENU resource (IDR_MAINFRAME) from the project, since adding and removing menu items is reflected in the menu's UI. It even appends the accelerators to the default menu items (e.g. Ctrl+O to the Open... item). However, the accelerator table (IDR_MAINFRAME) doesn't seem to be connected to the menu. Changing the default Ctrl+O for Open... to something like Ctrl+7 doesn't work. The Ctrl+O is still appended to the Open... menu item, and Ctrl+O still invokes the Open... handler. There is only one accelerator table in the resource file, so I have no idea where the CMFCMenuBar is getting its accelerator information.
At this point, I've been able to get the accelerators to work by manually loading the IDR_MAINFRAME accelerator table and translating messages myself. But the CMFCMenuBar is still appending the default accelerators.
Can anyone explain what is going on here? How can I force the MFCMenuBar to utilize my accelerator table and either not append the accelerators or append the correct accelerators?
回答1:
Having just spent far too much time wrestling with this problem I thought I'd post the solution that appears to work for me.
I call :
theApp.GetKeyboardManager()->ResetAll();
after the MDI framework stuff has been loaded from the registry (I've created a specific function called PostLoadFrame() which I call immediately after calling LoadFrame() which is where the registry seems to get read).
This function call ignores any accelerators loaded from the registry (which is fine for me because I don't allow customisation of them anyway) and uses the accelerators in your resource file instead.
Like I said, it seems to be working for me in my application. If you do allow customisation of the accelerator keys, then it looks like it's possible to merge your resource file and the registry stored values with other functions in CKeyboardManager, but it's a bit more complicated.
I hope that helps somebody somewhere save some of the time I've lost!!
回答2:
Apparently some CMFCMenuBar settings are persisted to the registry (HKCU I think.) Try clearing your application's registry settings and see if the correct accelerators are shown.
See this MSDN thread for some related CMFCMenuBar troubleshooting.
来源:https://stackoverflow.com/questions/1290752/why-is-cmfcmenubar-not-utilizing-the-accelerator-table