Click here to Skip to main content
11,706,159 members (49,550 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: MFC

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 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 at 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 at 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:
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)

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

  Print Answers RSS
0 OriginalGriff 196
1 Sergey Alexandrovich Kryukov 151
2 ppolymorphe 61
3 Inimicos 60
4 ProgramFOX 50
0 OriginalGriff 9,050
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,636

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 13 Apr 2013
Copyright © CodeProject, 1999-2015
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