GetMenuItemInfo & InsertMenuItem 枚举和追加菜单操作



int _AppendMenuOp(HMENU hDst, HMENU hSrc)
{
  int iCnt = 0;
  
  ASSERT(hDst && hSrc);

  for(int iSrc=0, iDst=GetMenuItemCount(hDst); iSrc<GetMenuItemCount(hSrc); iSrc++)
  {
    TCHAR szMenuStr[256] = {0};
    MENUITEMINFO mInfo = {0};
    mInfo.cbSize = sizeof(mInfo);
    mInfo.fMask = 0
      | MIIM_CHECKMARKS //Retrieves or sets the hbmpChecked and hbmpUnchecked members. 
      | MIIM_DATA //Retrieves or sets the dwItemData member. 
      | MIIM_ID //Retrieves or sets the wID member. 
      | MIIM_STATE //Retrieves or sets the fState member. 
      | MIIM_SUBMENU //Retrieves or sets the hSubMenu member. 
      | MIIM_TYPE //Retrieves or sets the fType and dwTypeData members. 
      | 0;
    mInfo.dwTypeData = szMenuStr;
    mInfo.cch = _countof(szMenuStr);

    VERIFY(GetMenuItemInfo(hSrc, iSrc, TRUE, &mInfo));

    if(mInfo.hSubMenu)
    {
      HMENU hSub = CreatePopupMenu();
      _AppendMenuOp(hSub, mInfo.hSubMenu);
      mInfo.hSubMenu = hSub;                    
    }

    InsertMenuItem(hDst, iDst++, TRUE,  &mInfo);
    iCnt++;
  }

  return iCnt;
}

void CDlg3Dlg::OnContextMenu(CWnd* pWnd, CPoint point) 
{
  // TODO: Add your message handler code here
  CMenu m1,m2, pm;
  m1.LoadMenu(IDR_MENU1);
  m2.LoadMenu(IDR_MENU2);

  pm.CreatePopupMenu();
  _AppendMenuOp(pm.m_hMenu, m1.m_hMenu);
  pm.AppendMenu(MF_SEPARATOR, 0, _T(""));
  _AppendMenuOp(pm.m_hMenu, m2.m_hMenu);
 
  pm.TrackPopupMenu(0, point.x, point.y, this);

}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭