Solved

Insert popup menu in mainframe menu

Posted on 1998-04-20
11
558 Views
Last Modified: 2013-11-20
Suppose that I have my main menu IDR_MAINFRAME and I would like to insert IDR_POPUP between 2nd and 3rd items of my main frame menu. How can I perform this?
IDR_MAINFRAME:
[File Edit Window Help]
          ^
     Insert here [Special]

IDR_POPUP:
[Special >][Item 1]
           [Item 2]
           [Item 3]
0
Comment
Question by:fmichaud061597
  • 5
  • 4
  • 2
11 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1300844
In the Resource Editor, there is a blank box next to Help.

                       ___
File Edit Window Help |   |
                       ---

Drag the box and drop it between Edit and Window. The right click the box and select Properties from the context menu.
0
 

Author Comment

by:fmichaud061597
ID: 1300845
It's not a design-time menu creation I wanted. It's a dynamic linkage between the main menu and an other popup menu. There are two menus: IDR_MAINFRAME and IDR_POPUP.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1300846
Sorry, I misunderstood.

You can do so by using the following steps:

1. Call CWnd::GetMenu() on the frame window;
2. Call CMenu::LoadMenu() to load the IDR_POPUP menu;
3. Call CMenu::InsertMenu() to insert the menu to the frame window menu;
4. Call CWnd::DrawMenuBar() to redraws the menu bar.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:fmichaud061597
ID: 1300847
I can't figure out what i'm doing wrong. This is my code:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    CMenu* pMenu = GetMenu();
    CMenu popupMenu;
    popupMenu.LoadMenu(IDR_POPUP);
    CString strMenu;
    popupMenu.GetMenuString(0, strMenu, MF_BYPOSITION);
    pMenu->InsertMenu(2, MF_BYPOSITION | MF_POPUP, (UINT)&popupMenu, strMenu);

    return 0;
}
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1300848
Ok here's the solution:

Move the menu modifying code to a separate message handler and make a small change to the InsertMenu() call and add the line DrawMenuBar():

LRESULT CMainFrame::OnApp10(WPARAM, LPARAM)
{
    CMenu* pMenu = GetMenu();
    CMenu popupMenu;
    popupMenu.LoadMenu(IDR_POPUP);
    CString strMenu;
    popupMenu.GetMenuString(0, strMenu, MF_BYPOSITION);

    pMenu->InsertMenu(1, MF_BYPOSITION | MF_POPUP, (UINT)popupMenu.GetSubMenu(0)->Detach(), strMenu);

      DrawMenuBar( );

      return 0;
}

in your WM_CREATE handler post a user message:

PostMessage(WM_APP+10);

Insert this line in the messagemap. (Just above END_MESSAGE_MAP()):

ON_MESSAGE(WM_APP+10, OnApp10)

Insert the declaration of the function int til the class header:

LRESULT OnApp10(WPARAM, LPARAM);

0
 

Author Comment

by:fmichaud061597
ID: 1300849
It's causes an error (Debug assertion failed) when I click on the new created menu in the menu bar.
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1300850
Then, whats the location of the assert failed line?
0
 

Author Comment

by:fmichaud061597
ID: 1300851
     { ASSERT(::IsMenu(m_hMenu)); return CMenu::FromHandle(::GetSubMenu(m_hMenu, nPos)); }

0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1300852
Do not use popupMenu as a local variable. Declare it as a member variable of CMainFrame.
0
 

Author Comment

by:fmichaud061597
ID: 1300853
Thank you. I was eye blinded =) So, one little more thing... if I want to access my popup menu from a button, for example, I need to invoke TracPopupMenu. But how to get exact x and y position for the menu, because I want that the menu appears just below my button and an at exactly place.

ex:
[Button]
[Button]
[Menu popup-----]
[Menu popup-----]
[Menu popup-----]
[Menu popup-----]
[Menu popup-----]
0
 
LVL 23

Expert Comment

by:chensu
ID: 1300854
The x and y parameters of TrackPopupMenu are in screen coordinates. You should know where you have put your buttons in the window. Call CWnd::ClientToScreen to convert them to screen coordinates. That's it.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

792 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