In part one of this series, Mitch described how to create a simple MFC based application skeleton. In this article, he builds on part one, and adds a menu to the applications main window. He also talks about accelerator keys.
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
class CApp : public CWinApp
Next, double-click on menu.cpp and enter the following code:
Create(NULL, "My Test Menu Application");
m_pMainWnd = new CMainWin;
I wont describe the code above because it was covered in part one of this series.