Liam P
Liam P

Reputation: 217

Create a function that selects a Main Menu bar based on the selected tab

I am fairly new to c++ and am wondering if there is a way to create a function that will select a menu bar created in the MFC Menu Editor and display it at the top of the window.

enter image description here

The idea is to have a different menu bar for each of the tabs because each tab will have different options.

For example a menu bar called ID_REGMENUBAR for Doom Reg and ID_SCRIPTMENUBAR for the Script

If more info is needed please say so. Thanks!

Upvotes: 1

Views: 327

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31629

Use CMenu::LoadMenu and CWnd::SetMenu. For example, declare member data:

CMenu m_menu1, m_menu2;

Initialize the menu once:

m_menu1.LoadMenu(ID_REGMENUBAR);
m_menu2.LoadMenu(ID_SCRIPTMENUBAR);

Use SetMenu(&m_menu1) to assign the menu on run time. You can respond to tab selection changes by looking TCN_SELCHANGE

BEGIN_MESSAGE_MAP(...)
    ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnTabSelection)
END_MESSAGE_MAP()

void CMyWnd::OnTabSelection(NMHDR*, LRESULT*)
{
    int tab = m_Tab.GetCurSel();
    CMenu *pMenu = NULL;
    if (tab == 0) pMenu = &m_menu1;
    if (tab == 1) pMenu = &m_menu2;
    CFrameWnd* frame = (CFrameWnd*)AfxGetMainWnd();
    frame->SetMenu(pMenu);
}

Upvotes: 1

Related Questions