Click here to Skip to main content
11,932,734 members (54,010 online)
Rate this:
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 13-Apr-13 0:35am
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:
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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.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