Using MFC in C++ Part 2: Menus - Loading the menu
(Page 5 of 9 )
To load the menu, we need to double-click on menu.cpp and change the constructor of the derived CMainWin class, as shown below:
CMainWin::CMainWin()
{
Create(NULL, "My Test Menu Application", WS_OVERLAPPEDWINDOW,
rectDefault, NULL, "MyMenu");
}We are adding changing the parameters passed to the Create() function, which creates an instance of our main window. The new parameters are:
WS_OVERLAPPEDWINDOW: The style of our window. Other styles include WS_VSCROLL, WS_HSCROLL and WS_EX_TOOLWINDOW. The styles can be Ored together to use more than one, i.e.: WS_OVERLAPPEDWINDOW | WS_VSCROLL for a normal window with a vertical scroll bar.
rectDefault: This is a RECT structure which contains the location of where our window will be loaded when it is create: a top, left, right and bottom co-ordinate. To set the location of the window manually, create a new MFC RECT object above the create() line, like this:
RECT r;
r.top = r.left = 100;
r.bottom = r.right = 300;Next, change the rectDefault parameter to match your newly created RECT object, r.
“MyMenu”: This is the name of our menu, enclosed by quotes. This will load our menu and display it as part of our apps main window. Compile and run your app (Cntl+F5). It should now have a menu, like this:

Notice how our menu options can’t be selected as yet? This is because they don’t contain any code to link them to a specific function when they are clicked on. Let’s add that code now.
Next: Linking up our new menus >>
More C++ Articles
More By Mitchell Harper