How to Create Menus in MFC

The MFC Library offers 2 methods of creating menus. This tutorial focuses on the Resource option. The other is the Dynamic Menu option. The tutorial example given builds upon the code given in the eHow article "How to Create Message Maps in MFC." Be sure to read that tutorial, if you haven't done so, as "MFC Message Maps" is prerequisite to menus.

Things You'll Need

  • Microsoft Visual Studio IDE
Show More

Instructions

    • 1

      Launch Microsoft Visual Studio. Wait until it loads.

    • 2

      Start an empty project without any default files. Click "File," "New" and click the "Projects" tab. Select "Win32 Application" and give it any name in the "Project name" text box. Click "OK." In the next page of the wizard, select "An empty project" and click "Finish." Click "OK."

    • 3

      Add a source file to the project. In the top menu bar click Project, "Add To Project" and "New." Highlight "C++ Source File", type any name for the file under "File name" and click "OK."

    • 4

      Copy and paste the following code onto the source file:

      #include LTafxwin.hGT

      #include "resource.h"



      class CProjWindow :public CFrameWnd

      {

      CMenu menu1;

      public:

      CProjWindow()

      {

      Create(NULL,"MFC CoderSource Window");

      menu1.LoadMenu(IDR_MENU1);

      SetMenu(&menu1);

      }

      void OnFileNew();



      DECLARE_MESSAGE_MAP()

      };



      BEGIN_MESSAGE_MAP( CProjWindow, CFrameWnd)

      ON_COMMAND(IDM_FILE_NEW,OnFileNew)

      END_MESSAGE_MAP()



      void CProjWindow::OnFileNew()

      {

      MessageBox("Clicked File->New");

      }

      class MyApp :public CWinApp

      {

      CProjWindow *wnd;

      public:

      BOOL InitInstance()

      {

      wnd = new CProjWindow();

      m_pMainWnd = wnd;

      m_pMainWnd->ShowWindow(1);

      return 1;

      }

      };



      MyApp theApp;



      In the first line of the code, replace "LT" with the "open angle bracket" symbol and "GT" with the "close angle bracket" symbol, in your source file.
    • 5

      Choose the MFC Library. In the top menu bar click "Project," "Settings," click the "General" tab and under "Microsoft Foundation Classes" select "Use MFC as Shared Library".

    • 6

      Create the menu. In the top menu bar click "Insert" and "Resource." Select "Menu" and click "New." A menu bar has been inserted on the top of the page. Highlight the first menu item at the very left. Type "File" in the caption field of the property box. Under "File" type "New". Select ID_FILE_NEW in the ID field of the property box. Hit Enter to close the properties box. A resource header file has been created.

    • 7

      Click Project, "Add To Project," then Files and select both "Script1.rc" and "resource.h."

    • 8

      Insert the lines of Step 4 in the appropriate places of the generated source code.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured