Home arrow C++ arrow Page 6 - 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 - Linking up our new menus
(Page 6 of 9 )

To make our menu items “clickable” and to give them a purpose, we need to add some code to our app to let the C++ compiler know that we want to execute a certain function whenever a specific menu item is clicked on.

Change the CMainWin() class declaration in menu.h, like it’s shown below:

class CMainWin : public CFrameWnd




afx_msg void OnItem1();

afx_msg void OnItem2();

afx_msg void OnItem3();



class CApp : public CWinApp



BOOL InitInstance();


As you can see, we have added three new lines. They are MFC function declarations, which will be called whenever a menu item is clicked on. Now, we need to modify the menu.cpp file. Firstly, we need to include our ids.h file:

#include "ids.h"

Next, modify the BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() lines, to look like this:






We have added three ON_COMMAND() macros to the message map for our main window. They tell the C++ compiler which messages our app will respond to. The ON_COMMAND macro takes two parameters: The first is the id of the message our app will “catch” (as defined in ids.h), and the second is the name of the function to execute whenever that message is “caught” from windows).

Therefore, whenever our app receives an IDM_1 message from windows, it will execute the CMainWin::OnItem1() function, etc.
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