Solved

Context menu

Posted on 2004-08-09
4
1,920 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
Window placement 17 80
pre4 challenge 19 100
Is there a simple front-end menu system. 9 88
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

805 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