6.3.1 快捷菜单
工程-添加到工程-Components and Controls,从这里可可以选择VC提供的一些组件和控件,添加一个POPUP MENU,选择所属于View类,此时View类中便添加了一个OnContextMenu函数。
01 void CMenuView::OnContextMenu(CWnd*, CPoint point)
02 {
03 // CG: This block was added by the Pop-up Menu component
04 {
05 if (point.x == -1 && point.y == -1){
06 //keystroke invocation
07 CRect rect;
08 GetClientRect(rect);
09 ClientToScreen(rect);
10 point = rect.TopLeft();
11 point.Offset(5, 5);
12 }
13 CMenu menu;
14 VERIFY(menu.LoadMenu(CG_IDR_POPUP_MENU_VIEW));
15 CMenu* pPopup = menu.GetSubMenu(0);
16 ASSERT(pPopup != NULL);
17 CWnd* pWndPopupOwner = this;
18 while (pWndPopupOwner->GetStyle() & WS_CHILD)
19 pWndPopupOwner = pWndPopupOwner->GetParent();
20 pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
21 pWndPopupOwner);
22 }
23 }
运行之后,在View类区域右键,会出现一个弹出窗口。