在開發時經常要建立菜單,並且動態顯示和隱藏菜單或者是某個子功能表。以下就是實現的方法:
1。建立並顯示菜單,先在資源裡添加菜單,然後如下代碼
SHMENUBARINFO mbi;
ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
mbi.cbSize=sizeof(SHMENUBARINFO);
mbi.hwndParent=m_hWnd;
mbi.hInstRes=::AfxGetInstanceHandle(); mbi.dwFlags=SHCMBF_HMENU|SHCMBF_HIDESIPBUTTON;//隱藏SIP mbi.nToolBarId=IDR_MYMENU; SHCreateMenuBar(&mbi);
2。隱藏菜單 HWND hwnd=::SHFindMenuBar(m_hWnd); if(hwnd!=NULL) ::ShowWindow(hwnd,SW_HIDE);
3。隱藏某個子功能表
TBBUTTONINFO tbbi = {0};
tbbi.cbSize = sizeof(TBBUTTONINFO);
tbbi.dwMask = TBIF_LPARAM | TBIF_BYINDEX;
::SendMessageW(mbi.hwndMB, TB_GETBUTTONINFO,1, (LPARAM)&tbbi); //1為SoftKey1,2為SoftKey2 g_hMenu = (HMENU)tbbi.lParam;//g_hMenu為全域變數,這樣就可以在任何地方隱藏子功能表了 EnableMenuItem(g_Menu,ID_TEST,MF_GRAYED);//隱藏子功能表
//顯示子功能表 EnableMenuItem(g_Menu,ID_TEST,MF_ENABLED);
4。使某個SoftKey不可用 ::SHEnableSoftkey(mbi.hwndMB,0,TRUE,FALSE); 5。動態修改菜單 ModifyMenu(g_hMenu,ID_TEST,MF_DISABLED|MF_STRING,ID_TEST,L"修改"); DrawMenuBar();