Microsoft KB Archive/131368

= How to create MFC applications that do not have a menu bar in Visual C++ =

Article ID: 131368

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ 2005 Express Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q131368

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

Note Microsoft Visual C++ 2005 supports both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model.

<div class="summary_section">

SUMMARY
For most Windows-based applications, a menu bar is a part of the user interface. The menu bar provides a functionality summary for the person using the program. However, it is not required that every Windows-based application must contain a menu bar. This article describes how to create an MFC application that does not have a menu bar.

For Windows-based applications generated by AppWizard, the IDR_MAINFRAME menu resource is the standard menu resource for both SDI and MDI applications. It is the only menu resource for an SDI application. MDI applications contain additional menus for each type of MDI child window they support. Those menu resources are usually named IDR_xxxTYPE, where xxx is related to the name of the corresponding document type. Thus, creating an application with no menus is not as easy for an MDI application as for an SDI application. You basically have to modify all functions related to loading and switching menus.

<div class="moreinformation_section">

Steps to Create SDI Application that Has No Menu Bar
<ol> <li>Generate an SDI application with AppWizard. Do not delete the IDR_MAINFRAME menu resource. If you have an application that was not generated with AppWizard, do not delete the corresponding main menu resource. Leaving the menu resource is required to avoid assertion failures in the MFC code.</li> <li> To prevent the main application window from having a menu bar, delete the already loaded menu, and set the hMenu field of the CREATESTRUCT structure to NULL in the CFrameWnd::PreCreateWindow function: BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) {        if(cs.hMenu!=NULL) {            ::DestroyMenu(cs.hMenu);      // delete menu if loaded cs.hMenu = NULL;             // no menu for this window }

return CFrameWnd::PreCreateWindow(cs); }                       </li></ol>

Steps to Create MDI Application that Has No Menu Bar
<ol> <li>Generate an MDI application with AppWizard. Do not delete the IDR_MAINFRAME menu resource. If you have an application that was not generated with AppWizard, do not delete the corresponding main menu resource. Leaving the menu resource is required to avoid assertion failures in the MFC code.</li> <li>Delete menu resources associated with MDI child windows (IDR_xxxTYPE). They are not used. By deleting them, you avoid a resource (memory) leak.</li> <li> Override the PreCreateWindow function for the CMainFrame class: BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) {        if(cs.hMenu!=NULL) {            ::DestroyMenu(cs.hMenu);      // delete menu if loaded cs.hMenu = NULL;             // no menu for this window }

return CMDIFrameWnd::PreCreateWindow(cs); }                       </li> <li> Modify the code responsible for switching menus by overriding the LoadFrame and OnCreateClient methods of CMainFrame. This is necessary because MFC has already loaded and switched menus automatically. The following shows what must be done: // Overridden method declarations for CMainFrame

BOOL LoadFrame( UINT nIDResource,            DWORD dwDefaultStyle = WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,             CWnd* pParentWnd = NULL, CCreateContext* pContext = NULL ); BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs,            CCreateContext* /*pContext*/);

// Overridden method declarations for CMainFrame

BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,            CWnd* pParentWnd, CCreateContext* pContext) {         return CFrameWnd::LoadFrame(nIDResource,dwDefaultStyle,              pParentWnd,pContext); }

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs,             CCreateContext* /*pContext*/) {         return CreateClient(lpcs,NULL); }                       </li></ol>

NOTE: Instead of calling the base class (CMDIFrameWnd) in the override of LoadFrame, you call its base class, CFrameWnd, instead. That way you can avoid the code that deals with MDI menus.

Keywords: kbdocview kbhowto kbmenu kbuidesign KB131368

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.