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 - A sample menu (Page 4 of 9 )
To put all of our theory into practice, lets create a menu. Double click on menu.rc and enter the following code:
#include <afxres.h>
#include "ids.h"
MyMenu MENU
{
POPUP "Popup"
{
MENUITEM "Item One", IDM_1
MENUITEM "Item Two", IDM_2
MENUITEM SEPARATOR
MENUITEM "Item Three", IDM_3
}
}
You’ll notice that we are #including two files: afxres.h, which is an MFC header file that contains several menu constants and macros. Secondly, ids.h, which will containe the definitions of our menu items id values. Notice how our menu items have id’s IDM_1, IDM_2 and IDM_3? We define these in ids.h. Create a new C++ header file (File -> New… C++ Header File) named ids.h. Into ids.h, enter the following code:
#define IDM_1 101
#define IDM_2 102
#define IDM_3 103
This simply creates three id variables with numeric values 101, 102 and 103 respectively. They are prefixed with IDM, which is representational for Identification Menu. We prefix all of our menu id’s with IDM.
Simply compiling and running the app will not add the menu to our program. To add it, we will need to modify the CMainWin() constructor.