?
Solved

Context menu

Posted on 2004-08-09
4
Medium Priority
?
1,929 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 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

764 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