Solved

Context menu

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

25 Experts available now in Live!

Get 1:1 Help Now