1. Defined in the header file (.h)
#define IDM_MY_MENU_1 101 //Menu ID
2. Declare the CMenu variable in the header file
CMenu pmenu;
3. Define the function in the header file
// Generated message map functions
//{
{AFX_MSG(CxxxDlg)
afx_msg void MyMenu();//define function
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
4. In the source file initialization menu (.cpp)
pmenu.CreatePopupMenu();
pmenu.AppendMenu(MF_STRING, IDM_MY_MENU_1, "&My Menu");
5. Add a demand instruction in the source file
BEGIN_MESSAGE_MAP(CxxxDlg, CDialog)
//{
{AFX_MSG_MAP(CxxxDlg)
ON_COMMAND(IDM_MY_MENU_1, MyMenu)//添加
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
6. Add a function to the source file
void CxxxDlg::MyMenu()
{ MessageBox("My right-click menu test"); }
7. Press the corresponding right mouse button
CPoint ptMenu;
GetCursorPos(&ptMenu);//The screen coordinates of the mouse point
pmenu.TrackPopupMenu(TPM_RIGHTBUTTON, ptMenu.x, ptMenu.y, this);