动态创建菜单

来源:互联网 发布:网络推广广告语 编辑:程序博客网 时间:2024/05/05 19:39
C/C++ code
void CMyF1Dlg::OnContextMenu(CWnd* pWnd , CPoint point ){ if (point.x == -1 && point.y == -1) { CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu subMenu; subMenu.CreatePopupMenu();//创建一个子菜单 subMenu.InsertMenu( 0, MF_BYPOSITION, 4444, _T("E") ); //给子菜单添加项 subMenu.InsertMenu( 0, MF_BYPOSITION, 5555 ,_T("F") ); subMenu.InsertMenu( 0, MF_BYPOSITION, 6666 ,_T("G") ); subMenu.InsertMenu( 0, MF_BYPOSITION, 7777 ,_T("H") ); CMenu pMenu; pMenu.CreatePopupMenu(); //创建一个主菜单 pMenu.AppendMenu( MF_STRING, 1111, _T("A") ); //给主菜单添加三个基本相 pMenu.AppendMenu( MF_STRING, 2222, _T("B") ); pMenu.AppendMenu( MF_STRING, 3333, _T("C") ); pMenu.AppendMenu( MF_POPUP,( UINT_PTR )subMenu.m_hMenu,_T( "M") ); //添加一个带弹出菜单的项 CWnd* pWndPopupOwner = this; while ( pWndPopupOwner->GetStyle() & WS_CHILD ) pWndPopupOwner = pWndPopupOwner->GetParent(); pMenu.TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner );}




原创粉丝点击