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 menus
SHMENUBARINFO mbi;
ZeroMemory (& mbi, sizeof (SHMENUBARINFO ));
Mbi. cbSize = sizeof (SHMENUBARINFO );
Mbi. hwndParent = m_hWnd;
Mbi. hInstRes =: AfxGetInstanceHandle ();
Mbi. dwFlags = SHCMBF_HMENU | SHCMBF_HIDESIPBUTTON; // hide the SIP
Mbi. nToolBarId = IDR_MYMENU;
SHCreateMenuBar (& mbi );
2. Hide menu
HWND 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 SK1, 2 is SK2
G_hMenu = (HMENU) tbbi. lParam; // g_hMenu is the global variable, so that the sub-menu can be hidden anywhere.
EnableMenuItem (g_Menu, ID_TEST, MF_GRAYED); // hide the sub-menu
// Display the sub-menu EnableMenuItem (g_Menu, ID_TEST, MF_ENABLED );
4. Make a SK unavailable
: SHEnableSoftkey (mbi. hwndMB, 0, TRUE, FALSE );