[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 564
  • Last Modified:

Insert popup menu in mainframe menu

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
fmichaud061597
Asked:
fmichaud061597
  • 5
  • 4
  • 2
1 Solution
 
chensuCommented:
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
 
fmichaud061597Author Commented:
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
 
chensuCommented:
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
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!

 
fmichaud061597Author Commented:
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
 
piano_boxerCommented:
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
 
fmichaud061597Author Commented:
It's causes an error (Debug assertion failed) when I click on the new created menu in the menu bar.
0
 
piano_boxerCommented:
Then, whats the location of the assert failed line?
0
 
fmichaud061597Author Commented:
     { ASSERT(::IsMenu(m_hMenu)); return CMenu::FromHandle(::GetSubMenu(m_hMenu, nPos)); }

0
 
chensuCommented:
Do not use popupMenu as a local variable. Declare it as a member variable of CMainFrame.
0
 
fmichaud061597Author Commented:
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
 
chensuCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now