1
-----增加右键弹出菜单方法一:
Project-->Add to Project-->Components and Controls -->Visual C++ Components
-->Pop-up menu 注意要加到View类中,不要加到Frame类中,因为Frmae被View覆盖,收不到鼠标消息.
2
-----增加右键弹出菜单方法二: 模仿一,在鼠标右键响应函数中加载自己创建的资源menu,用TrackPopupMenu弹出菜单
3 消息响应顺序: 对"显示"添加消息响应函数,在CMenuView和CMainFrame类中都添加
void CMenuView::OnMenuitemShow()
{
// TODO: Add your command handler code here
MessageBox("CMenuView::OnMenuitemShow()");
}
void CMainFrame::OnMenuitemShow()
{
// TODO: Add your command handler code here
MessageBox("CMainFrame::OnMenuitemShow()");
}
// pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,this);
//最后一个参数用 this --- 就是把菜单的拥有者指定为CMenuView,那就只能是CMenuView可以响应菜单项的COMMAND消息
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,GetParent());
//最后一个参数用 GetParent()---拥有者为框架,那么框架CMainFrame就可以响应菜单的COMMAND消息
// 但是前提是CMenuView中没有设计该菜单项COMMAND消息,才会轮到CMainFrame响应
// 这就是 消息响应顺序: 所有的子类中都没有设计响应函数,才会轮到父类.
//--- 不知道为什么给"显示"添加了COMMAND响应后,"退出"变灰色了....
//---
//---
//---
// MenuView.cpp : implementation of the CMenuView class
//
#include "stdafx.h"
#include "Menu.h"
#include "MenuDoc.h"
#include "MenuView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMenuView
IMPLEMENT_DYNCREATE(CMenuView, CView)
BEGIN_MESSAGE_MAP(CMenuView, CView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CMenuView)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_MENUITEM_SHOW, OnMenuitemShow)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMenuView construction/destruction
CMenuView::CMenuView()
{
// TODO: add construction code here
}
CMenuView::~CMenuView()
{
}
BOOL CMenuView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMenuView drawing
void CMenuView::OnDraw(CDC* pDC)
{
CMenuDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMenuView printing
BOOL CMenuView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMenuView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMenuView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMenuView diagnostics
#ifdef _DEBUG
void CMenuView::AssertValid() const
{
CView::AssertValid();
}
void CMenuView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMenuDoc* CMenuView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMenuDoc)));
return (CMenuDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMenuView message handlers
//DEL void CMenuView::OnTest()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL MessageBox("MenuView clicked");
//DEL }
//DEL void CMenuView::OnUpdateEditCut(CCmdUI* pCmdUI)
//DEL {
//DEL // TODO: Add your command update UI handler code here
//DEL // pCmdUI->Enable();
//DEL }
// -----增加右键弹出菜单方法一:
// Project-->Add to Project-->Components and Controls -->Visual C++ Components
// -->Pop-up menu 注意要加到View类中,不要加到Frame类中,因为Frmae被View覆盖,收不到鼠标消息.
#include "resource.h"
void CMenuView::OnContextMenu(CWnd*, CPoint point)
{
// CG: This block was added by the Pop-up Menu component
{
if (point.x == -1 && point.y == -1){
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(CG_IDR_POPUP_MENU_VIEW));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}
}
// -----增加右键弹出菜单方法二: 模仿一,在鼠标右键响应函数中加载自己创建的资源menu,用TrackPopupMenu弹出菜单
void CMenuView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 仿照上面的void CMenuView::OnContextMenu(CWnd*, CPoint point)编写
CMenu menu;
menu.LoadMenu(IDR_MENU1);
CMenu *pPopup = menu.GetSubMenu(0);//获取右键弹出菜单的子菜单,只有一个子菜单,必须是0
ClientToScreen(&point); //把客户区坐标转换为屏幕坐标
// pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,this);
//最后一个参数用 this --- 就是把菜单的拥有者指定为CMenuView,那就只能是CMenuView可以响应菜单项的COMMAND消息
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,GetParent());
//最后一个参数用 GetParent()---拥有者为框架,那么框架CMainFrame就可以响应菜单的COMMAND消息
// 但是前提是CMenuView中没有设计该菜单项COMMAND消息,才会轮到CMainFrame响应
// 这就是 消息响应顺序: 所有的子类中都没有设计响应函数,才会轮到父类.
CView::OnRButtonDown(nFlags, point);
}
// 除非把这个函数删除,否则CMainFrame中的void CMainFrame::OnMenuitemShow() 是没有机会响应的.
void CMenuView::OnMenuitemShow()
{
// TODO: Add your command handler code here
MessageBox("CMenuView::OnMenuitemShow()");
}
本文介绍了两种在VC++中创建右键弹出菜单的方法:通过Visual C++ Components添加到View类,以及在鼠标右键响应函数中加载自定义资源并使用TrackPopupMenu。详细讨论了消息响应顺序,如何在CMenuView和CMainFrame类中添加消息处理函数,并解释了菜单项COMMAND消息的响应规则。此外,还提到了在实现过程中遇到的‘退出’菜单项变为灰色的问题。
1210

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



