参考 http://blog.csdn.net/csdnzhwk/article/details/47395639
参考 http://blog.csdn.net/jiadabin/article/details/22686161
1、创建一个基于对话框的项目
2、在资源头文件(Resource.h),定义命令ID:
#define ID_MENUCAT 1109 #define ID_MENUDOG 1110 #define ID_MENUMONKEY 1111 #define _APS_NEXT_CONTROL_VALUE 1112
3、在需要弹出快捷菜单的地方创建菜单(ParticipateMeeting.cpp)
CMenu menu; menu.CreatePopupMenu(); //创建一个弹出菜单 menu.AppendMenu(MF_BYCOMMAND | MF_STRING, ID_MENUCAT, _T("猫")); //添加子菜单项 menu.AppendMenu(MF_SEPARATOR); //分隔线 menu.AppendMenu(MF_BYCOMMAND | MF_STRING, ID_MENUDOG, _T("狗")); menu.AppendMenu(MF_BYCOMMAND | MF_STRING, ID_MENUMONKEY, _T("猴")); CPoint pt; GetCursorPos(&pt); menu.TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, this);
4、在对话框的头文件(ParticipateMeeting.h)中声明菜单的消息处理函数
afx_msg void OnMenuCat(); afx_msg void OnMenuDog(); afx_msg void OnMenuMonkey();
5、在对话框的源文件(ParticipateMeeting.cpp)中添加消息映射宏,将命令ID关联到消息处理函数中:
ON_COMMAND(ID_MENUCAT, OnMenuCat) ON_COMMAND(ID_MENUDOG, OnMenuDog) ON_COMMAND(ID_MENUMONKEY, OnMenuMonkey)
6、在对话框源文件(ParticipateMeeting.cpp)中添加消息处理函数的实现代码:
void ParticipateMeeting::OnMenuCat() { MessageBox(_T("猫菜单被按下!")); } void ParticipateMeeting::OnMenuDog() { MessageBox(_T("狗菜单被按下!")); } void ParticipateMeeting::OnMenuMonkey() { MessageBox(_T("猴菜单被按下!")); }
时间: 2024-10-13 06:30:05