MFC teaches you how to write pop-up menus

Source: Internet
Author: User


In VS2008, it is not possible to add pop-up menus as vc++6.0 through compnents and controls, which need to be added manually. You can create pop-up menus in two ways.

First, the use of existing menu resources to create

(1) First create a new menu resource under menu in Resource View.


(2) Then edit the new menu to look like the pop-up menu you want.




(3) Add the Wm_rbuttondown message to the CView class and add the following code in the message function Onrbuttondown ():

[Cpp]view plaincopyprint?

1 void Cmenuview::onrbuttondown (UINT nflags, CPoint Point)

2 {

3//TODO: Add the message Handler code here and/or call the default value

4

5//Create pop-up menu with existing menu resources

6 CMenu menu;

7 menu. LOADMENUW (IDR_MENU1); To load an existing menu resource

8 CMenu *ppopupmenu = menu. GetSubMenu (0); Get to create a good pop-up menu

9 ClientToScreen (&point); Converts the coordinates of the client area to the screen coordinates

//ppopupmenu->trackpopupmenu (Tpm_leftalign, Point.x, Point.y, GetParent ());

One Ppopupmenu->trackpopupmenu (tpm_leftalign | Tpm_rightbutton, Point.x, Point.y, this); Mouse Right click pop-up menu

12

Cview::onrbuttondown (nflags, point);

14}

void Cmenuview::onrbuttondown (UINT nflags, CPoint Point)

{

TODO: Add the message Handler code here and/or call the default value

Create a pop-up menu with existing menu resources

CMenu menu;

Menu. LOADMENUW (IDR_MENU1); To load an existing menu resource

CMenu *ppopupmenu = menu. GetSubMenu (0); Get to create a good pop-up menu

ClientToScreen (&point); Converts the coordinates of the client area to the screen coordinates

Ppopupmenu->trackpopupmenu (Tpm_leftalign, Point.x, Point.y, GetParent ());

Ppopupmenu->trackpopupmenu (Tpm_leftalign | Tpm_rightbutton, Point.x, Point.y, this); Mouse Right click pop-up menu

Cview::onrbuttondown (nflags, point);

}

Second, dynamically create pop-up menu

Creating a pop-up menu dynamically refers to the creation of a complete code type, adding the following code to the Wm_rbuttondown message:

[Cpp]view plaincopyprint?

void Cmenuview::onrbuttondown (UINT nflags, CPoint Point)

16 {

/TODO: Add Message Handler code here and/or invoke default values

18//Create pop-up menu dynamically

CMenu menu;

menu. CreatePopupMenu (); Create a pop-up menu

menu. APPENDMENUW (Mf_bycommand | Mf_string, Id_right_clear, _t ("Shear (&x)")); Add Child menu item

menu. APPENDMENUW (Mf_bycommand | Mf_string, Id_right_copy, _t ("Copy (&c)"));

The menu. APPENDMENUW (Mf_bycommand | Mf_string, Id_right_paste, _t ("Paste (&p)"));

menu. APPENDMENUW (Mf_separator);

menu. APPENDMENUW (Mf_bycommand | Mf_string, Id_right_clear, _t ("Clear \tctrl + C"));

ClientToScreen (&point); Converts the coordinates of the client area to the coordinates of the screen

27

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.