Solved

Insert popup menu in mainframe menu

Posted on 1998-04-20
11
561 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
[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
  • 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
Independent Software Vendors: 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!

 

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

Independent Software Vendors: 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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

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