HowTo: Add pop-up menu

Source: Internet
Author: User
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)



</

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.