VC++60-MFC学习-右键菜单-RMenu

源码访问链接:

gitee:   https://gitee.com/piazini/MFC-stu
github:  https://github.com/piazini/MFC-stu

 

编译器:

VC++ 6.0 (SP6) 简体中文企业版

 

项目名:RMenu 

( 如何新建项目 ?  --> VC++60新建MFC框架程序 - 对话框程序 )

 

成品截图:

 

代码:

RMenuDlg.h

class CRMenuDlg : public CDialog
{
protected:
	//右键菜单
	afx_msg void On_32772();  //右键-大家好
	afx_msg void On_32773();  //右键-同学好
	afx_msg void On_32774();  //右键-你好
};

 

RMenuDlg.cpp

//注意是MAP里的参数是CRMenuDlg里填写,不是上面的CAboutDlg里填写
BEGIN_MESSAGE_MAP(CRMenuDlg, CDialog)
	// 菜单栏代码需要手动添加
	//菜单栏
	ON_COMMAND(ID_MENUITEM32772, On_32772)  //右键-大家好
	ON_COMMAND(ID_MENUITEM32773, On_32773)  //右键-同学好
	ON_COMMAND(ID_MENUITEM32774, On_32774)  //右键-你好

END_MESSAGE_MAP()

//鼠标右键弹起
void CRMenuDlg::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	//载入菜单资源IDR_MENU1
	CMenu menu;
	menu.LoadMenu(IDR_MENU1);
	
	//获取菜单项里的子菜单
	CMenu *pPopup = menu.GetSubMenu(NULL);

	//获取鼠标点击位置
	CPoint pointPos;
	GetCursorPos(&pointPos);

	//弹出菜单
	pPopup ->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pointPos.x, pointPos.y,this);


	CDialog::OnRButtonUp(nFlags, point);
}

//右键-大家好
void CRMenuDlg::On_32772() 
{
	MessageBox(_T("On_32772"));
}  

//右键-同学好 
void CRMenuDlg::On_32773()  
{
	MessageBox(_T("On_32773"));
}  

//右键-你好
void CRMenuDlg::On_32774() 
{
	MessageBox(_T("On_32774"));
}  

  

 

 

参考:

https://blog.csdn.net/weixin_43694353/article/details/84935050

黑马MFC 三天课程

posted @ 2022-06-09 22:16  悟透  阅读(419)  评论(0)    收藏  举报