Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

Regds
SNI
Posted

Take a look at this step-by-step explanation: http://www.functionx.com/visualc/howto/contextmenu.htm[^]


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)
 
Share this answer
 
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:
C++
void CMyListCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_MYLIST_POPUP_MENU));
    CMenu *pSub = menu.GetSubMenu(0);
    // Modify menu items here if necessary (e.g. gray out items)
    int nCmd = pSub->TrackPopupMenuEx(
        TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_RETURNCMD | TPM_NONOTIFY,
        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().
 
Share this answer
 
Comments
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: http://www.codeproject.com/Articles/2354/Owner-Drawn-Menu-with-Icons-Titles-and-Shading
Premnath Mali 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.
Premnath Mali 22-Feb-17 7:01am    
I just wanted to show on right click anywhere in the dialog please help

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900