鼠标右键菜单:
CMenu menu;
menu.LoadMenu(IDR_MENU1);
CMenu *popup=menu.GetSubMenu(0);
ClientToScreen(&point);
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,this);
CView::OnRButtonDown(nFlags, point);
代码:
void CCMenuView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
if(nChar==0x0d)
{
if (++m_nCount==0)
{
m_Menu.CreatePopupMenu();
GetParent()->GetMenu()->AppendMenu(MF_POPUP,(UINT)m_Menu.m_hMenu,"PhoneBook");
GetParent()->DrawMenuBar();/*Whenever a menu that resides in a window is changed (whether or not the window is displayed), the application should call CWnd::DrawMenuBar*/
}else
{
m_Menu.AppendMenu(MF_STRING,IDM_PHONE1+m_nCount-1,m_strline.Left(m_strline.Find(' ')));
stringArray.Add(m_strline);
m_strline.Empty();
Invalidate();
}
}else
{
m_strline+=nChar;
dc.TextOut(0,0,m_strline);
}
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CCMenuView::OnPhone1()
{
MessageBox("view test");
}void CMainFrame::OnUpdateEditCut(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable();
}
BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
UINT nID = LOWORD(wParam);
CCMenuView *pview=(CCMenuView*)GetActiveView();
if (nID>=IDM_PHONE1 && nID<IDM_PHONE1+pview->stringArray.GetSize())
{
CClientDC dc(pview);
dc.TextOut(0,0,pview->stringArray.GetAt(nID-IDM_PHONE1));
return TRUE;
}
return CFrameWnd::OnCommand(wParam, lParam);
}

本文详细介绍了如何在C++应用中通过代码实现鼠标右键菜单和弹出菜单的功能,包括加载菜单、屏幕定位和响应右键点击事件。
6114

被折叠的 条评论
为什么被折叠?



