[VC ++] how to add right-click menu in Win32 program and add response

Source: Internet
Author: User

0. How to open the console in the Win32 program and copy the following code:

# Include "Io. H "# include" fcntl. H "Void initconsole () {int nret = 0; file * FP; allocconsole (); // call the window nret = _ open_osfhandle (long) getstdhandle (std_output_handle) that generates the console ), _ o_text); // getstdhandle gets the console handle. If there is no allocconsole (), obtain the standard input/output window handle fp = _ fdopen (nret, "W "); // fdopen () converts the nret file description to the corresponding file pointer and returns * stdout = * FP; setvbuf (stdout, null, _ ionbf, 0 ); // output stream}

The following describes how to right-click a menu in Win32. The steps are as follows:

1: first define three IDM Resources in the header file. These three parameters are very important and will be used when listening for menu click events.

#define IDM_AAA 200#define IDM_BBB 201#define IDM_CCC 202

2. define a global variable

static HMENU hMenu;

3. Add the code in the init () method:

hMenu=CreatePopupMenu();AppendMenu(hMenu,MF_STRING,IDM_AAA,_T("&AAA"));AppendMenu(hMenu,MF_SEPARATOR,0,NULL);AppendMenu(hMenu,MF_STRING,IDM_BBB,_T("&BBB"));AppendMenu(hMenu,MF_STRING,IDM_CCC,_T("&CCC"));


4. Add the right-click response code in wm_rbuttondown of the callback function wndproc.

case WM_RBUTTONDOWN:{    printf("Right");    POINT point;    point.x=LOWORD(lParam);    point.y=HIWORD(lParam);    ClientToScreen(hWnd,&point);    TrackPopupMenu(hMenu,TPM_RIGHTALIGN,point.x,point.y,0,hWnd,NULL);    break;}

You can also set the status of this field:

CheckMenuItem(hMenu,IDM_AAA,MF_CHECKED);

5. Add the response from the selected menu to the wm_command of the callback function wndproc.
To facilitate the test, add the following code:

cout<<wParam<<endl;

Right-click and view the output table. The selected macro-defined value is displayed. The switch type of this value can be processed without mentioning the table.

0. How to open the console in the Win32 program and copy the following code:

# Include "Io. H "# include" fcntl. H "Void initconsole () {int nret = 0; file * FP; allocconsole (); // call the window nret = _ open_osfhandle (long) getstdhandle (std_output_handle) that generates the console ), _ o_text); // getstdhandle gets the console handle. If there is no allocconsole (), obtain the standard input/output window handle fp = _ fdopen (nret, "W "); // fdopen () converts the nret file description to the corresponding file pointer and returns * stdout = * FP; setvbuf (stdout, null, _ ionbf, 0 ); // output stream}

The following describes how to right-click a menu in Win32. The steps are as follows:

1: first define three IDM Resources in the header file. These three parameters are very important and will be used when listening for menu click events.

#define IDM_AAA 200#define IDM_BBB 201#define IDM_CCC 202

2. define a global variable

static HMENU hMenu;

3. Add the code in the init () method:

hMenu=CreatePopupMenu();AppendMenu(hMenu,MF_STRING,IDM_AAA,_T("&AAA"));AppendMenu(hMenu,MF_SEPARATOR,0,NULL);AppendMenu(hMenu,MF_STRING,IDM_BBB,_T("&BBB"));AppendMenu(hMenu,MF_STRING,IDM_CCC,_T("&CCC"));


4. Add the right-click response code in wm_rbuttondown of the callback function wndproc.

case WM_RBUTTONDOWN:{    printf("Right");    POINT point;    point.x=LOWORD(lParam);    point.y=HIWORD(lParam);    ClientToScreen(hWnd,&point);    TrackPopupMenu(hMenu,TPM_RIGHTALIGN,point.x,point.y,0,hWnd,NULL);    break;}

You can also set the status of this field:

CheckMenuItem(hMenu,IDM_AAA,MF_CHECKED);

5. Add the response from the selected menu to the wm_command of the callback function wndproc.
To facilitate the test, add the following code:

cout<<wParam<<endl;

Right-click and view the output table. The selected macro-defined value is displayed. The switch type of this value can be processed without mentioning the table.

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.