Solved

Context menu

Posted on 2004-08-09
4
1,887 Views
Last Modified: 2013-11-20
I want to show main application menu as context menu. Consider SDI Doc-View application created by MFC Application Wizard. I add the following function to it:

void CMenuTestView::OnContextMenu(CWnd* pWnd, CPoint point)
{
    CMenu menu;
    menu.LoadMenu(IDR_MAINFRAME);

    menu.TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, AfxGetMainWnd());
}

It shows the context menu, but first menu level (File, View, Edit ...) is shown without text. How can I change this code to get right result ? Menu resource should not be changed, only code. Thanks.
0
Comment
Question by:AlexFM
  • 3
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11760043
  CMenu menuMain;
   menuMain.LoadMenu(IDR_MAINFRAME);
   CString s;
   menuMain.GetMenuString(0, s, MF_BYPOSITION);


   CMenu menu;
   menu.CreatePopupMenu();
   menu.AppendMenu(MF_STRING, 12345, (LPCTSTR)s);


    menu.TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, AfxGetMainWnd());


I think you will need to load the main menu and copy the contents to a new menu item by item
0
 
LVL 48

Author Comment

by:AlexFM
ID: 11760119
Maybe, but this code cannot be used as prototype. File is added as menu item and not as popup menu. I still need full solution.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11760277
This is some more

      CMenu menuMain;
      menuMain.LoadMenu(IDR_MAINFRAME);
      CString s;
      UINT nID;


      CMenu menu;
      CMenu subMenu;
      menu.CreatePopupMenu();

      menuMain.GetMenuString(0, s, MF_BYPOSITION);
      nID = menuMain.GetMenuItemID(0);
      if(nID == -1)
      {
            CMenu* pPopup = menuMain.GetSubMenu(0);

            subMenu.CreateMenu();
            
            menu.AppendMenu(MF_POPUP, (UINT)subMenu.GetSafeHmenu(), (LPCTSTR)s);


            pPopup->GetMenuString(0, s, MF_BYPOSITION);
            nID = pPopup->GetMenuItemID(0);
            subMenu.AppendMenu(MF_STRING, nID, (LPCTSTR)s);
      }
      else
            menu.AppendMenu(MF_STRING, nID, (LPCTSTR)s);


      menu.TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, AfxGetMainWnd());



You will need some recursive routine and obviously need to create menus with the new keyword.  It will also require more work to cope with separators, check marks etc.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 11760324
A bit more to demonstrate the principle

   CMenu menuMain;
   menuMain.LoadMenu(IDR_MAINFRAME);
   CString s;
   UINT nID;


      CMenu menu;
      CMenu subMenu;
      CMenu subMenu2;
      menu.CreatePopupMenu();

      menuMain.GetMenuString(0, s, MF_BYPOSITION);
      nID = menuMain.GetMenuItemID(0);
      if(nID == -1)
      {
            CMenu* pPopup = menuMain.GetSubMenu(0);

            subMenu.CreateMenu();
            
            menu.AppendMenu(MF_POPUP, (UINT)subMenu.GetSafeHmenu(), (LPCTSTR)s);


            pPopup->GetMenuString(0, s, MF_BYPOSITION);
            nID = pPopup->GetMenuItemID(0);
            subMenu.AppendMenu(MF_STRING, nID, (LPCTSTR)s);

            pPopup->GetMenuString(1, s, MF_BYPOSITION);
            nID = pPopup->GetMenuItemID(1);
            subMenu.AppendMenu(MF_STRING, nID, (LPCTSTR)s);
      }
      else
            menu.AppendMenu(MF_STRING, nID, (LPCTSTR)s);


      menuMain.GetMenuString(1, s, MF_BYPOSITION);
      nID = menuMain.GetMenuItemID(1);
      if(nID == -1)
      {
            CMenu* pPopup = menuMain.GetSubMenu(1);

            subMenu2.CreateMenu();
            
            menu.AppendMenu(MF_POPUP, (UINT)subMenu2.GetSafeHmenu(), (LPCTSTR)s);


            pPopup->GetMenuString(0, s, MF_BYPOSITION);
            nID = pPopup->GetMenuItemID(0);
            subMenu2.AppendMenu(MF_STRING, nID, (LPCTSTR)s);

            pPopup->GetMenuString(1, s, MF_BYPOSITION);
            nID = pPopup->GetMenuItemID(1);
            subMenu2.AppendMenu(MF_STRING, nID, (LPCTSTR)s);
      }
      else
            menu.AppendMenu(MF_STRING, nID, (LPCTSTR)s);


    menu.TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, AfxGetMainWnd());
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now