码迷,mamicode.com
首页 > 编程语言 > 详细

【MFC】VS2013 动态创建快捷菜单(右键菜单)

时间:2016-12-23 16:49:51      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:关联   cpp   target   pos   sep   this   append   enum   函数   

参考 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("猴菜单被按下!"));
}

 

【MFC】VS2013 动态创建快捷菜单(右键菜单)

标签:关联   cpp   target   pos   sep   this   append   enum   函数   

原文地址:http://www.cnblogs.com/happykoukou/p/6214995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!