Using MFC in C++ Part 2: Menus - The resource file
(Page 2 of 9 )
In C++, objects such as buttons, dialog boxes, menus, list boxes and radio buttons are created as part of a resource file. A resource file contains a resource script, which is compiled at run-time by a resource compiler. The resource compiler is typically bundled with the C++ IDE. In this article, I will be using Microsoft Visual C++ 6 and its built in resource compiler to discuss menus.
You should be familiar with a menu. A menu is simply a column of buttons that can be activated by clicking on them. When they are clicked on, a popup menu may be displayed showing further options. These options can also be popup menus in themselves, and can contain a further set of options, etc.
To demonstrate how we will incorporate a menu into our C++ project via a resource file, open Microsoft Visual C++ 6 and create a new empty Win32 project as described in article 1. Name the project “MenuApp”.
Right click on the “MenuApp classes” node under the ClassView tab, and add a new class. Name the new class “menu”, as shown below:

Click on the FileView tab and expand all of the nodes. Double click on menu.h and then menu.cpp and delete all of the code in the right hand window. To re-create our basic application skeleton, enter the following code into menu.h:
class CMainWin : public CFrameWnd
{
public:
CMainWin();
DECLARE_MESSAGE_MAP()
};
class CApp : public CWinApp
{
public:
BOOL InitInstance();
};Next, double-click on menu.cpp and enter the following code:
#include <afxwin.h>
#include "menu.h"
CMainWin::CMainWin()
{
Create(NULL, "My Test Menu Application");
}
BOOL CApp::InitInstance()
{
m_pMainWnd = new CMainWin;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
END_MESSAGE_MAP()
CApp App;I wont describe the code above because it was covered in part one of this series.
Next: Adding a resource file to our project >>
More C++ Articles
More By Mitchell Harper