首先是需要一个MENU,然后TrackPopupMenu The TrackPopupMenu function displays a shortcut menu at the specified location and tracks the selection of items on the menu. The shortcut menu can appear anywhere on the screen.To specify an area of the screen the menu should not overlap, use the TrackPopupMenuEx function. BOOL TrackPopupMenu( HMENU hMenu, // handle to shortcut menu UINT uFlags, // options int x, // horizontal position int y, // vertical position int nReserved, // reserved, must be zero HWND hWnd, // handle to owner window CONST RECT *prcRect // ignored );
The TrackPopupMenu function displays a shortcut menu at the specified location and tracks the selection of items on the menu. The shortcut menu can appear anywhere on the screen.To specify an area of the screen the menu should not overlap, use the TrackPopupMenuEx function. BOOL TrackPopupMenu(
HMENU hMenu, // handle to shortcut menu
UINT uFlags, // options
int x, // horizontal position
int y, // vertical position
int nReserved, // reserved, must be zero
HWND hWnd, // handle to owner window
CONST RECT *prcRect // ignored
);
menu.LoadMenu(IDM_MENU1);
menu.SubMenu(0).TrackPopupMenu(...)
{
//MessageBox("right button down"); CMenu pp; //加载菜单
pp.LoadMenu (IDR_MENU_ZKBH);
CMenu* pSubMenu;
//加载子菜单
pSubMenu = pp.GetSubMenu(0);
//显示菜单
ClientToScreen(&point);
pSubMenu->TrackPopupMenu(TPM_RIGHTBUTTON, point.x, point.y,this);
pSubMenu->DestroyMenu();}
正好有加载代码,也许有点作用。当然加载的前提是要求加入资源!三楼先!
1)需要一个MENU资源;
2)创建一个自己的List Box类,比如CMyList类,加入下面的事件
void CMyList::OnContextMenu(CWnd* pWnd, CPoint point)
{
GetParent()->SendMessage(WM_UserStepEditPopUp,(WPARAM)pWnd,(LPARAM)&point);
}
在上面是事件中加一个自定义消息。
#define WM_UserStepEditPopUp WM_USER+9
3)把自定义消息引入对话框类。在加如下代码:
LRESULT Cxxxx::OnEditPopUp(WPARAM wParam,LPARAM lParam)
{ CPoint *point = (CPoint* )lParam; m_pOperList.ShowControls(HIDDEN_CONTROL); CMenu menu;
SetFocus();
if(menu.LoadMenu(MAKEINTRESOURCE(IDR_STEP_OPER)))
{
CMenu *pMenu = menu.GetSubMenu(1);
if(NULL != pMenu)
{
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTALIGN|TPM_RIGHTBUTTON,point->x,point->y,this);
}
} return 0;
} 就可以了!