You often need to create menus during development and dynamically display and hide menus or a sub-menu. The following is the implementation method:
1. Create and display the menu. Add the menu in the resource first, and then run the following code:
Shmenubarinfo MBI;
Zeromemory (& MBI, sizeof (shmenubarinfo ));
MBI. cbsize = sizeof (shmenubarinfo );
MBI. hwndparent = m_hwnd;
MBI. hinstres =: AfxGetInstanceHandle (); MBI. dwflags = shcmbf_hmenu | hidden; // hide sip MBI. ntoolbarid = idr_mymenu; shcreatemenubar (& MBI );
2. Hide the menu hwnd =: shfindmenubar (m_hwnd); If (hwnd! = NULL): showwindow (hwnd, sw_hide );
3. Hide a sub-menu
Tbbuttoninfo tbbi = {0 };
Tbbi. cbsize = sizeof (tbbuttoninfo );
Tbbi. dwmask = tbif_lparam | tbif_byindex;
: Sendmessagew (MBI. hwndmb, tb_getbuttoninfo, 1, (lparam) & tbbi); // 1 is softkey1, 2 is softkey2 g_hmenu = (hmenu) tbbi. lparam; // g_hmenu is a global variable, so that the sub-menu enablemenuitem (g_menu, id_test, mf_grayed) can be hidden anywhere. // hide the sub-menu
// Display the sub-menu enablemenuitem (g_menu, id_test, mf_enabled );
4. Make a softkey unavailable: shenablesoftkey (MBI. hwndmb, 0, true, false); 5. Modifymenu (g_hmenu, id_test, mf_disabled | mf_string, id_test, l "modify"); drawmenubar ();