Dynamic addition, deletion and response of MFC menu items

xiaoxiao2021-03-06  107

Dynamic addition and dynamic response to menu items when writing a game map editor, which records some of the issues and solutions that come up. Because the game map is a hierarchical storage and display, there is a Layer menu in the editor main menu. In addition to a "show" menu item and a sperator, other menu items are based on the map The number of layers and the name of the layer is dynamically added, so the number of menu items cannot be known in the compile period, or the ON_COMMAND macro will be associated with the command processing function. There is also an on_command_range macro that can associate an ID within a range with a Command Handler, but if this is, I have to specify the largest ID number in this macro. Because I don't know how many layers in the actual map, I don't want the dynamically added menu item to have a maximum on the ID. The first menu item of the Layer menu is "show online", which responded to the update_command_ui message, I first delete all the menu items after "Show Only" in this response function, and check how many layers actually have actually, get the map Name, add menu items according to the actual number of layers. The code that deletes the menu item is as follows: nitemcount = PlayerMenu-> getMenuItemcount (); for (i = 2; i deleteMenu (i, mf_byposition);} There is a problem with this deletion. Because I am deleted according to the location of the menu item, and each time a PlayerMenu-> DeleteMenu (I, MF_BYPOSITION) will be less than one menu item, the location offset of the subsequent menu item will be reduced, and the result of final deletion is Not I expected. The solution is to delete with mf_bycommand according to ID, or NITEMCOUITEMCOUNT (); for (i = 2; i deleteMenu (2, mf_byposition);} I can guarantee deletion All the menu items on the last dynamic added. Dynamically add menu items I was doing: nLayerCount = pMaterialDoc-> GetLayerCount (); for (i = 0; i AppendMenu (MF_STRING | MF_ENABLED | MF_CHECKED, ID_LAYER_SHOW_ONLY i 1, pMaterialDoc -> getLayername (i));} But the menu item to add on this is all gray, even if I use the MF_Enabled option. Because the MFC will automatically let the menu items corresponding to the COMMAND HANDLER and the corresponding menu items. This is controlled by the m_bautomenuenable variable in cframewnd. When it is TRUE, the MFC makes the menu item should be grayed, and it is made by our own. I only hope that the menu items I have dynamically added are enabled, and the ENABLE problem of other menus is processed by the MFC.

转载请注明原文地址:https://www.9cbs.com/read-99479.html

New Post(0)