HowTo: Add a pop-up menu

Source: Internet
Author: User

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)



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.