Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC
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 13-Apr-13 0:35am
SNI1.5K
Rate this: bad
good
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;
    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().
  Permalink  
Comments
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: http://www.codeproject.com/Articles/2354/Owner-Drawn-Menu-with-Icons-Titles-and-Shading
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


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