Home arrow C++ arrow Page 5 - Using MFC in C++ Part 2: Menus

Using MFC in C++ Part 2: Menus

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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 47
November 29, 2001
  1. · Using MFC in C++ Part 2: Menus
  2. · The resource file
  3. · Adding a resource file to our project
  4. · A sample menu
  5. · Loading the menu
  6. · Linking up our new menus
  7. · Adding the OnItem functions
  8. · Adding accelerator keys
  9. · Conclusion

print this article

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:

Our new menu and its disabled menu items

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.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials