自定义右键菜单:
#ifndef _CONTEXTMENU_H_
#define _CONTEXTMENU_H_
#include <windows.h> //动态菜单
#define ITEM_MENU_REPLY 40001
#define ITEM_MENU_ALLREPLAY 40002
#define ITEM_MENU_RELAY 40003
#define ITEM_MENU_DELETE 40004
#define ITEM_MENU_FINALDELETE 40005
#define ITEM_MEUN_MARK 40006
#define ITEM_SUBMENU_UNSEEN 40007
#define ITEM_SUBMENU_SEEN 40008 class CContextPopMenu
{
public:
CContextPopMenu();
~CContextPopMenu(); void PopMenu(HWND hwnd,int nX,int nY );
void PopMenu(HWND hwnd,const POINT& pt); void EnableMenu(int nItem,bool nEnable);
private:
HMENU m_pPopMenu;
};
#endif
#include "ContextMenu.h" CContextPopMenu::CContextPopMenu()
{
m_pPopMenu = CreatePopupMenu();
if (m_pPopMenu)
{
HMENU Submenu = ::CreatePopupMenu();
::InsertMenu(m_pPopMenu,(-),MF_BYPOSITION,ITEM_MENU_REPLY,TEXT("回复"));
::InsertMenu(m_pPopMenu,(-),MF_BYPOSITION,ITEM_MENU_ALLREPLAY,TEXT("全部回复"));
::InsertMenu(m_pPopMenu,(-),MF_BYPOSITION,ITEM_MENU_RELAY,TEXT("转发"));
::InsertMenu(m_pPopMenu,(-),MF_BYPOSITION,ITEM_MENU_DELETE,TEXT("删除"));
::InsertMenu(m_pPopMenu,(-),MF_BYPOSITION,ITEM_MENU_FINALDELETE,TEXT("彻底删除"));
::InsertMenu(m_pPopMenu,(-),MF_BYPOSITION|MF_POPUP,(unsigned int)Submenu,TEXT("标记"));
::InsertMenu(Submenu,(-),MF_BYPOSITION|MF_POPUP,ITEM_SUBMENU_UNSEEN,TEXT("未读邮件"));
::InsertMenu(Submenu,(-),MF_BYPOSITION|MF_POPUP,ITEM_SUBMENU_SEEN,TEXT("已读邮件"));
}
} CContextPopMenu::~CContextPopMenu()
{
if (m_pPopMenu)
{
DestroyMenu(m_pPopMenu);
m_pPopMenu = NULL;
}
} void CContextPopMenu::PopMenu(HWND hwnd,int nX,int nY )
{
if (m_pPopMenu && hwnd)
{
TrackPopupMenu(m_pPopMenu,TPM_LEFTALIGN|TPM_RIGHTBUTTON,nX,nY,,hwnd,NULL)
}
} void CContextPopMenu::PopMenu(HWND hwnd,const POINT& pt)
{
PopMenu(hwnd,pt.x,pt.y);
}
然后在WndProc回调函数,响应WM_RBUTTONUP函数时创建
case WM_RBUTTONUP:
POINT pt;
::GetCursorPos(&pt);
CContextPopMenu* pPopMenu = new CContextPopMenu;
pPopMenu->PopMenu(hwnd,pt);
break;
对每个菜单项的响应在WM_COMMAND消息里处理
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ITEM_MENU_REPLY:
MessageBox(NULL,TEXT("响应回复菜单项"),TEXT("提示"),MB_OK);
break;;
case ITEM_SUBMENU_UNSEEN:
MessageBox(NULL,TEXT("响应标记子菜单项"),TEXT("提示"),MB_OK);
break;
}