Environment:
Visual C + + 6.0
Windows xp
Mfc
dialog-based Application
1. Declare a CMenu type of variable, as follows:
CMenu Popmenu;
2. Call CMenu:: CreatePopupMenu () function
Popmenu. CreatePopupMenu ();
-->3. -->and then inresouce.hfile, define
#define Id_menuitem_hello 32772
It should be noted that it is best toresouce.hin the file"Next default values for new objects"predefined Blockin the_aps_next_command_valuethe value is modified to32772+1, namely32773,
#define _aps_next_command_value 32773
because this may be in useWizardautomatic generation of code is caused by a symbol conflict.
4. !--[endif]--> then calls cmenu::appendmenu (), to menu Riga menu item
Popmenu. AppendMenu (mf_string, Id_menuitem_hello, "HELLO World");
Mf_string refers to a menu item expressed as a string, with the string content as a parameter 3 .
Id_menuitem_hello is in resouce.h The resource code defined in the file.
"Hello world!" The is the string that will be displayed in the menu.
5. in the Dialog in the declaration, add the corresponding Command Handler
afx_msg void Onmenuitemhello ();
Add a definition to implement
void Cxxxdlg::onmenuitemhello ()
{
setwindowtext ("Hello World");
}
-->6. -->because we implement the user right click the mouse pop-up menu, so join the messagewm_ Rbuttondownof theHandler, and inHandlercalled inCMenu:: TrackPopupMenu (. ..)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 noteworthy here thatCWndResponseWm_rbuttondownOf the returnedCPointparameters ofxandycorresponds to the coordinate system where the upper-left corner of the corresponding form is the origin, andTrackPopupMenuin the functionX,ycorresponds to the coordinate system of the original point in the upper-left corner of the screen, so you need to convert here.
7. compile run, when we click on the pop-up menu " Hello World "Item, dialog box Caption column is modified to " Hello World ".
End (Workbook)
</