Home arrow C++ arrow Page 8 - 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 - Adding accelerator keys
(Page 8 of 9 )

Accelerator keys allow a user to access menu items quicker by pressing a pre-defined combination of keys, instead of having to click on a specific menu item everytime it is requred. An accelerator table, which contains a list accelerator keys, has the following structure:



KeyCode, Message Id [, Type, Options]

KeyCode, Message Id [, Type, Options]

KeyCode, Message Id [, Type, Options]


KeyCode: The KeyCode value should contain the key combination that will trigger the accelerator. The key code can be either:
  • Any alphanumeric character enclosed in quotes, such as “a”, “C”, “6”. These are case sensitive.
  • The header file afxres.h defines several macros for “virtual keys”. Virtual key macros allow keys such as F1 and F2 (VK_F1 to VK_F12) to be used as accelerator keys. If a virtual key is used as the key code, then the type should be set to VIRTKEY.
  • The ASCII code number of a character. If an ASCII key code is used, then the type should be set to ASCII.
Message Id: The message Id should contain an ID declared as part of an ON_COMMAND() macro for a windows message map. For example, our application would use IDM_1, IDM_2 and IDM_3.

Type: Can be either empty, VIRTKEY or ASCII, as described above.

Options: If CONTROL is specified, then the control key must be pressed to trigger this accelerator key. If SHIFT is specified, then the shift key must be pressed to trigger this accelerator key. If ALT is specified, then the alt key must be pressed to trigger this accelerator key.

Adding an accelerator key for a menu item is easy. To make the CMainWin::OnItem1() function execute when we press the Control key and the 1 (one) key, we would add the following code to the end of our resource file, menu.rc:





To trigger the CMainWin::OnItem2() function when we prress the F2 key, we would add this accelerator key:


To trigger our the CMainWin::OnItem3() function when we press the “3” key, we would add this accelerator key:

51, IDM_3, ASCII // 51 is the ascii code for the 3 key

We can’t actually use these accelerator keys yet. We need to load the accelerator table. Double click on menu.cpp and add the following line just before the end of the CMainWin constructor:


MessageBox("Couldn't load the accelerator table");

The LoadAccelTable function is a member of the CFrameWnd class, accepts one parameter, the name of the menu (in quotes), and returns a BOOL value. It loads our accelerator table into memory and sends the notification messages to our application when the right combination of keys is pressed.

To make sure our accelerator keys work, compile and run our app. First, press Cntl+1. Next, press the F2 key. Lastly, press just the “3” key. Each different OnItem function should be triggered.

To make our menu items display the accelerator keys they respond to, change the MENU table in menu.rc, like this:



POPUP "Popup"


MENUITEM "Item One\tCntl+1", IDM_1

MENUITEM "Item Two\tF2", IDM_2


MENUITEM "Item Three\t3", IDM_3



Our menu now contains each items accelerator key combination
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