Solved

Context menu

Posted on 2004-08-09
4
1,925 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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