Win32建立右键弹出菜单(PopMenu)

自定义右键菜单:

 1 #ifndef  _CONTEXTMENU_H_
 2 #define _CONTEXTMENU_H_
 3 #include <windows.h>
 4 
 5 
 6 //动态菜单
 7 #define  ITEM_MENU_REPLY  40001
 8 #define  ITEM_MENU_ALLREPLAY 40002
 9 #define  ITEM_MENU_RELAY 40003
10 #define  ITEM_MENU_DELETE 40004
11 #define  ITEM_MENU_FINALDELETE 40005
12 #define  ITEM_MEUN_MARK 40006
13 #define  ITEM_SUBMENU_UNSEEN 40007
14 #define ITEM_SUBMENU_SEEN 40008
15 
16 
17 class CContextPopMenu
18 {
19 public:
20     CContextPopMenu();
21     ~CContextPopMenu();
22 
23     void PopMenu(HWND hwnd,int nX,int nY );
24     void PopMenu(HWND hwnd,const POINT& pt);
25 
26     void EnableMenu(int nItem,bool nEnable);
27 private:
28     HMENU m_pPopMenu;
29 };
30 #endif

 

 1 #include "ContextMenu.h"
 2 
 3 CContextPopMenu::CContextPopMenu()
 4 {
 5     m_pPopMenu = CreatePopupMenu();
 6     if (m_pPopMenu)
 7     {
 8         HMENU Submenu = ::CreatePopupMenu();
 9         ::InsertMenu(m_pPopMenu,(-1),MF_BYPOSITION,ITEM_MENU_REPLY,TEXT("回复"));
10         ::InsertMenu(m_pPopMenu,(-1),MF_BYPOSITION,ITEM_MENU_ALLREPLAY,TEXT("全部回复"));
11         ::InsertMenu(m_pPopMenu,(-1),MF_BYPOSITION,ITEM_MENU_RELAY,TEXT("转发"));
12         ::InsertMenu(m_pPopMenu,(-1),MF_BYPOSITION,ITEM_MENU_DELETE,TEXT("删除"));
13         ::InsertMenu(m_pPopMenu,(-1),MF_BYPOSITION,ITEM_MENU_FINALDELETE,TEXT("彻底删除"));
14         ::InsertMenu(m_pPopMenu,(-1),MF_BYPOSITION|MF_POPUP,(unsigned int)Submenu,TEXT("标记"));
15         ::InsertMenu(Submenu,(-1),MF_BYPOSITION|MF_POPUP,ITEM_SUBMENU_UNSEEN,TEXT("未读邮件"));
16         ::InsertMenu(Submenu,(-1),MF_BYPOSITION|MF_POPUP,ITEM_SUBMENU_SEEN,TEXT("已读邮件"));
17     }
18 }
19 
20 CContextPopMenu::~CContextPopMenu()
21 {
22     if (m_pPopMenu)
23     {
24         DestroyMenu(m_pPopMenu);
25         m_pPopMenu = NULL;
26     }
27 }
28 
29 void CContextPopMenu::PopMenu(HWND hwnd,int nX,int nY )
30 {
31     if (m_pPopMenu && hwnd)
32     {
33         TrackPopupMenu(m_pPopMenu,TPM_LEFTALIGN|TPM_RIGHTBUTTON,nX,nY,0,hwnd,NULL)
34     }
35 }
36 
37 void CContextPopMenu::PopMenu(HWND hwnd,const POINT& pt)
38 {
39     PopMenu(hwnd,pt.x,pt.y);
40 }

 

然后在WndProc回调函数,响应WM_RBUTTONUP函数时创建

1 case WM_RBUTTONUP:
2     POINT pt;
3     ::GetCursorPos(&pt);
4     CContextPopMenu* pPopMenu = new CContextPopMenu;
5     pPopMenu->PopMenu(hwnd,pt);
6 break;

 

对每个菜单项的响应在WM_COMMAND消息里处理

 1 case WM_COMMAND:
 2         switch(LOWORD(wParam))
 3         {
 4         case ITEM_MENU_REPLY:
 5             MessageBox(NULL,TEXT("响应回复菜单项"),TEXT("提示"),MB_OK);
 6             break;;
 7         case ITEM_SUBMENU_UNSEEN:
 8             MessageBox(NULL,TEXT("响应标记子菜单项"),TEXT("提示"),MB_OK);
 9             break;
10         }

 借鉴他人代码:http://blog.csdn.net/bizhu12/article/details/6664210

Win32建立右键弹出菜单(PopMenu)

上一篇:node.js在windows下的学习笔记(4)---同步,异步,回调的概念


下一篇:photoshop利用滤镜及计算将人像打造出超酷的暗调金属质感效果