Solved

Insert popup menu in mainframe menu

Posted on 1998-04-20
11
555 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:fmichaud061597
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:fmichaud061597
Comment Utility
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
Comment Utility
Then, whats the location of the assert failed line?
0
 

Author Comment

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

0
 
LVL 23

Accepted Solution

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

Author Comment

by:fmichaud061597
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
configuration management 2 99
Host to IP 7 73
ffmpeg - "rtsp://...... Operation not permitted" 4 68
withoutTen challenge 14 88
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 …
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

11 Experts available now in Live!

Get 1:1 Help Now