0

I would like to add sub options to the Main Menu at the top of a main frame window in MFC.

For example; File>Open, or Edit>Undo.

Top ToolBar

Is this possible to do at all? My intention is to replace the function of some buttons in my program with options typically found in the drop down menus

Also after adding an item to the Main Menu how would you use it to call a function?

Liam P
  • 217
  • 5
  • 13
  • Your image looks like the main menu of a frame and not a tool bar. Are you asking how to add to the main menu? – rrirower May 24 '16 at 17:27
  • Yes sorry that is what i meant. Couldn't remember what it was called. – Liam P May 24 '16 at 17:28
  • You should read the MSDN docs on menu creation via the resource editor and dynamically. – rrirower May 24 '16 at 17:34
  • I figured out how to add the options i want but im not sure how to add functionality to them. Right now i have File>Open but it is grayed out. I added the MENUITEM to the `.rc` file to achieve this – Liam P May 24 '16 at 17:58
  • Please reference [this post](http://stackoverflow.com/questions/25508413/menu-items-are-being-enabled-or-disabled-by-default-why) for more information since your question can be interpreted as a duplicate of that post. – rrirower May 24 '16 at 18:03
  • Thank you for the reference. I don't mean to be posting a duplicate, but I still am not able to get it to do what i want. I have been trying to use [this](http://stackoverflow.com/questions/3673546/dynamic-menu-using-mfc) but even doing that doesn't create a sub menu for me. – Liam P May 24 '16 at 18:58
  • There is a relevant question here: Are you using the new MFC Feature Pack classes, by the way of a `CMFCMenuBar` inside a `CFrameWndEx` or, on other hand, are you using the traditional menu inside a traditional `CFrameWnd`? The way the menu bar and its items are handled is completely different! – sergiol May 25 '16 at 11:09
  • I am using the traditional CFrameWnd. – Liam P May 25 '16 at 12:59

1 Answers1

1

Here I have added an entry to the View menu called Test:

Add menu item

When I build and runt he program it shows disabled:

Disabled

This is because I still have to create an event handler. In the resource editor you right-click the menu item and select Add Event handler:

Add

This brings up the class wizard:

Class wizard

On the dialogue there are a couple of menu event handlers to choose from. Select the one you need (as in the screen shot) but don't forget to choose the right class on the right. Then click Add and Edit.

Now you can add your event handler functionality. Example:

void CMainFrame::OnViewTest()
{
    AfxMessageBox(_T("Hello!"), MB_OK | MB_ICONINFORMATION);
}

When I compile and run this:

Popup message

Hopefully this will help you get up and running.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    I actually just stumbled across this searching through VS but you perfectly described the solution. Awesome answer thanks so much! – Liam P May 25 '16 at 20:24