孙鑫VC++深入详解:Lesson6 Part3 ---创建右键弹出菜单 TrackPopupMenu

来源:互联网 发布:csv导入mysql 编辑:程序博客网 时间:2024/06/05 00:49


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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMenuViewIMPLEMENT_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 commandsON_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/destructionCMenuView::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 csreturn CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CMenuView drawingvoid CMenuView::OnDraw(CDC* pDC){CMenuDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here}/////////////////////////////////////////////////////////////////////////////// CMenuView printingBOOL CMenuView::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn 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 _DEBUGvoid 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 invocationCRect 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);//获取右键弹出菜单的子菜单,只有一个子菜单,必须是0ClientToScreen(&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()");}




原创粉丝点击