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 - 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:
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:
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.