#include <afxwin.h>
#define ID_NEW 2002
#define ID_EXIT 2004
#define IDM_MENUITEM1 2005
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
void OnFileNew();
void OnExit();
afx_msg void OnRButtonDown( UINT, CPoint );
DECLARE_MESSAGE_MAP()
};
BOOL CSimpleApp::InitInstance(){
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}
CMainFrame::CMainFrame()
{
Create(NULL, "MFC Menu Example");
//Create main menu
CMenu *menu = new CMenu();
menu->CreateMenu();
//creare 2 submenu's
CMenu *subMenu1 = new CMenu();
CMenu *subMenu2 = new CMenu();
//make submenu popup
subMenu1->CreatePopupMenu();
subMenu2->CreatePopupMenu();
//append items to submenu
menu->AppendMenu(MF_POPUP|MF_STRING, (UINT)subMenu1->m_hMenu, TEXT("File") );
menu->AppendMenu(MF_POPUP|MF_STRING, (UINT)subMenu2->m_hMenu, TEXT("Help") );
subMenu1->AppendMenu(MF_STRING, ID_NEW, TEXT("New"));
subMenu1->AppendMenu(MF_STRING, ID_EXIT, TEXT("Exit"));
SetMenu(menu);
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_COMMAND(ID_NEW ,OnFileNew)
ON_COMMAND(ID_EXIT ,OnExit)
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
END_MESSAGE_MAP()
CSimpleApp MFCApp1;
//respond to menu selection new by creating message box
afx_msg void CMainFrame::OnFileNew()
{
MessageBox(TEXT("New button clicked"),TEXT("menu demo"),MB_OK);
}
afx_msg void CMainFrame::OnExit()
{
PostMessage(WM_CLOSE,0,0);
}
//display context menu
afx_msg void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CMenu *contextmenu;
contextmenu = new CMenu();
contextmenu->CreatePopupMenu();//= CreatePopupMenu();
contextmenu->AppendMenu(MF_STRING,ID_NEW, TEXT("New"));
contextmenu->AppendMenu(MF_STRING,ID_EXIT, TEXT("Exit"));
ClientToScreen(&point);
contextmenu->TrackPopupMenu(TPM_RIGHTBUTTON,point.x,point.y, this, NULL);
}