在开发时经常要创建菜单,并且动态显示和隐藏菜单或者是某个子菜单。以下就是实现的方法:

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为SK1,2为SK2
   g_hMenu = (HMENU)tbbi.lParam;//g_hMenu为全局变量,这样就可以在任何地方隐藏子菜单了

 EnableMenuItem(g_Menu,ID_TEST,MF_GRAYED);//隐藏子菜单
//显示子菜单 EnableMenuItem(g_Menu,ID_TEST,MF_ENABLED);

4。使某个SK不可用
::SHEnableSoftkey(mbi.hwndMB,0,TRUE,FALSE);
posted on 2007-05-10 19:51  小镇  阅读(2170)  评论(0编辑  收藏  举报