Environment:
Visual C ++ 6.0
Windows XP
MFC
Dialog-Based Application
 
 1. Declare a CMenu type variable as follows:
CMenu popmenu; 
 
 2. Call the CMenu: CreatePopupMenu () function.
Popmenu. CreatePopupMenu (); 
 
 3. Then, in the resouce. h file, define
# Define ID_MENUITEM_HELLO 32772
Note that it is best to change the value of _ APS_NEXT_COMMAND_VALUE in the pre-defined block of "Next default values for new objects" in the resouce. h file to 32772 + 1, that is, 32773,
# Define _ APS_NEXT_COMMAND_VALUE 32773
This may cause code conflicts when you use Wizard to automatically generate code.
 
 
 4. Then, call CMenu: AppendMenu () to add a Menu item to the Menu.
Popmenu. AppendMenu (MF_STRING, ID_MENUITEM_HELLO, "Hello world ");
MF_STRING indicates that the menu item is expressed as a string. The content of the string is parameter 3.
ID_MENUITEM_HELLO is the resource code defined in the resouce. h file.
"Hello world !" Is the string that will be displayed in the menu. 
 
 5. Add the corresponding command handler to the Dialog declaration.
Afx_msg void OnMenuitemHELLO ();
Add definition in implement
Void CXXXDlg: OnMenuitemHELLO ()
{
SetWindowText ("Hello world ");
} 
 
 6. Because we implement right-click the user to bring up the menu, add the handler of the message WM _ RBUTTONDOWN and call CMenu: TrackPopupMenu (…) In the handler (...) Function: 
Void CXXXDlg: OnRButtonDown (UINT nFlags, CPoint point)
  {
CRect rect;
GetWindowRect (& rect );
Popmenu. TrackPopupMenu (TPM_LEFTALIGN, point. x + rect. TopLeft (). x, point. y + rect. TopLeft (). y, this/*, & rect */);
CDialog: OnRButtonDown (nFlags, point );
}
It is worth noting that x and y of the CPoint parameter returned by the CWnd response WM_RBUTTONDOWN correspond to the coordinate system at the origin in the upper left corner of the corresponding form, while x in the TrackPopupMenu function, y corresponds to the coordinate system with the origin in the upper left corner of the screen, so conversion is required here.
 
    
 
 7. Compile and run the program. After clicking the "Hello world" project in the pop-up menu, the "Caption" column of the dialog box is changed to "Hello world ".
   
 
 
 
 
Workbook)