 |
|
 |
I use function : MDISetMenu
so the replaceMenu fucntion become :
CMenu NewMenu;
NewMenu.LoadMenu(n_IDResource);
ASSERT(NewMenu);
MDISetMenu(&NewMenu, NULL);
And then no problem when i quit my application.
|
|
|
|
 |
|
 |
This is definitely a clean solution and MFC's way of switching between menus. This sets the necessary foundation when your application supports multiple "environments". In CAD world this is also often referred to as "Workbenches".
I would like to just one line to the above four lines to switch the menus;
CMenu NewMenu;
NewMenu.LoadMenu(n_IDResource);
ASSERT(NewMenu);
MDISetMenu(&NewMenu, NULL);
DrawMenuBar();
Best Wishes for the bright future,
Yogesh Dhakad
|
|
|
|
 |
|
 |
Hello to all the CP readers / community,
Since I mentioned about the "environment" and "workbenches". I would add the information on switching of the Toolbars based on the environments.
I will demonstrate here how would put a toolbar off (disappeared) in one environment and get it back when the user comes to the existing environment where we started;
void CMainFrame::OnSwitchTo_Environment1()
{
CMenu NewMenu;
NewMenu.LoadMenu(IDR_MAINFRAME);
ASSERT(NewMenu);
MDISetMenu(&NewMenu, NULL);
DrawMenuBar();
m_wndToolBar1.ShowWindow(SW_HIDE);
RecalcLayout();
}
void CMainFrame::OnSwitchTo_Environment2()
{
CMenu NewMenu;
NewMenu.LoadMenu(IDR_MFCTYPE);
ASSERT(NewMenu);
MDISetMenu(&NewMenu, NULL);
DrawMenuBar();
m_wndToolBar1.ShowWindow(SW_SHOW);
RecalcLayout();
}
It goes without saying that m_wndToolBar1 was created during the CMainFrame::OnCreate() like our defautl m_wndToolBar.
Best regards,
Yogesh Dhakad
|
|
|
|
 |
|
 |
If you don't do this then the CMenu will be lost and access violations will occure. The CMenu gets 'deleted' when you use the stack to store it, store it as a member in your CFrameWnd derived class and this will work fine.
|
|
|
|
 |
|
 |
Hi I tried to use your code and i encountered an access violation error when callinf ReplaceMenu which i made public from a view. Is there somthing i need to set?
www.dejavusoft.com
|
|
|
|
 |
|
 |
What happens if you call ReplaceMenu when the document is Maximized
and additional items sit in the MenuBar (maximaze,minimize etc. buttons)? After getting old menu back you never get those buttons...
any workaround ?
thanks in advance
|
|
|
|
 |
|
 |
I want to do a menu selection for two language by to take corresponding an instruction from IDR_MAINFRAME in MDI application(doc-view template) and have for all my view then the menu of corresponding language.
Choice the corresponding language make once at the begin work of application.
How I can to give it?
If I make to switch menu by choice one of two template for corresponding language then open saving file I get that opening template menu replaced work menu, but when I click at nonpersistent document to switch from one menu set to another is not. Help me, please
|
|
|
|
 |
|
 |
I want add a new menuitem at childframe,but I can't get the menu with GetMenu().Do u know how to do?
|
|
|
|
 |
|
 |
I haven't run your code, but when:
CMenu NewMenu;
goes out of scope, doesn't ~CMenu call DestroyMenu?
with the result being that CMainFrame::m_hMenuDefault is now a handle to a destroyed menu?
|
|
|
|
 |
|
 |
hrm.. the example which i found on msdn used m_NewMenu without defining it in the function.. i think perhaps in the .h there should be a variable CMenu m_NewMenu so that its destructor wont get called until CMainFrame goes away..
but.. i have jumped between several menus using this function and exited and there are no memory leaks, and it hasn't crashed on me yet.. although i think declaring the m_NewMenu in the .h may be a better idea..
thanks for the comment ill look into the destructor of CMenu!
-dz
|
|
|
|
 |
|
 |
No, notice in the code, the m_NewMenu is now set to the safe menu handle.
I find that the way Windows implements menus is really confusing, and MFC's CMenu is a really thin wrapper around the OS's API's. Every time I have to work with menus I get confused regarding the Attach(), Detach(), and DestroyMenu() calls.
I implement a lot of dynamic menus in my apps, so I created a whole menu manager class to do this sort of stuff. Here's a code snippet of swapping menus:
void MenuManager::UseMenu(const CString& menuName, const CString& frameName)
{
CFrameWnd* mf=winMgr->GetFrame(frameName);
CMenu* menu=menuList[menuName].BuildMenu();
mf->SetMenu(menu);
vector accelList;
menuList[menuName].BuildAccel(accelList);
if (accelList.size()!=0)
{
ACCEL* accel=new ACCEL[accelList.size()];
for (int i=0; i<(int)accelList.size(); i++)
{
accel[i]=accelList[i];
}
HACCEL h=CreateAcceleratorTable(accel, accelList.size());
delete accel;
if (mf->m_hAccelTable)
{
DestroyAcceleratorTable(mf->m_hAccelTable);
}
mf->m_hAccelTable=h;
}
menu->Detach();
delete menu;
}
Just thought I'd share.
Good article. I might suggest one that delves more into the nuances I mentioned above.
Marc
|
|
|
|
 |
|
 |
is there a way to move this article over to the section specifically on Menus?
-dz
|
|
|
|
 |
|
 |
Yes - edit the article by clicking on the 'Modify' link at the top and then choose a different section.
cheers,
Chris Maunder
|
|
|
|
 |