Click here to Skip to main content
13,097,225 members (65,744 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I have an application where I need to show context menu in List Control. Does anybody have idea how to show it.

Posted 12-Apr-13 23:35pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Take a look at this step-by-step explanation:[^]

Sometimes you may want to display a context menu. That is, if the user right-clicks, instead of a (left) click, you may want to display a particular menu. Many MFC controls don't provide this functionality on their own; you would have to apply the action on the parent control.

1.Start Microsoft Visual C++ MFC Application named ControlContext
2.Create it as Dialog Box without the AboutBox
3.Set the Dialog Title to Context-Sensitive Menu on Controls
4.Add a button to dialog box. Change its ID to IDC_SUBMIT_BTN and its Caption to Submit
5.Add a check box to the dialog box. Change its ID to IDC_APPROVED_CHK and its Caption to Approved: .... (and so long)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There is no difference between dialog and frame based applications regarding context menus of controls. To add a context menu to a list control, create the menu using the resource editor and add the ON_WM_CONTEXTMENU() / OnContextMenu() handler to your list control class:
void CMyListCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
    CMenu menu;
    CMenu *pSub = menu.GetSubMenu(0);
    // Modify menu items here if necessary (e.g. gray out items)
    int nCmd = pSub->TrackPopupMenuEx(
        point.x, point.y, AfxGetMainWnd(), NULL);
    if (nCmd)
        SendMessage(WM_COMMAND, nCmd);

Note that using AfxGetMainWnd() requires that the pointer to your main dialog window is assigned to the CWinApp member variable m_pMainWnd in InitInstance().
SNI 16-Apr-13 13:20pm
Thanks for ur reply. Is there any way to show background image for menu and changing text color for menu options.
Jochen Arndt 16-Apr-13 14:16pm
This requires owner drawn menus. Examples can be found searching for "cmenu owner draw" and "cmenu user draw". This CP article might help:
Member 12986299 22-Feb-17 7:00am
for this I'm getting error
Unhandled exception at 0x00fc8689 in MenuPratice.exe: 0xC0000005: Access violation reading location 0x00000004.
Jochen Arndt 22-Feb-17 7:19am
It is a general example about creating a context menu. It must be adapted to your project.

Ensure that:
- The menu resource exists (this is checked in debug builds using the VERIFY() macro)
- It is a valid menu resource (pSub will be NULL if not)
- Your CWinApp derived class has initialised the m_pMainWnd member variable (as clearly stated)

If you still have problems create a debug build and use the debugger to find which variable is out of range generating the access violation.

Because this is an old answered thread you should not use it to ask new questions. You might open your own question if you are still stuck.
Member 12986299 22-Feb-17 7:01am
I just wanted to show on right click anywhere in the dialog please help
Member 12986299 22-Feb-17 7:04am
instead of using submenu I used menu to track after that When I right click on the area, a thin rectangle pops up instead of full menu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 13 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100