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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Using MFC in C++ Part 2: Menus - The resource file
(Page 2 of 9 )

In C++, objects such as buttons, dialog boxes, menus, list boxes and radio buttons are created as part of a resource file. A resource file contains a resource script, which is compiled at run-time by a resource compiler. The resource compiler is typically bundled with the C++ IDE. In this article, I will be using Microsoft Visual C++ 6 and its built in resource compiler to discuss menus.

You should be familiar with a menu. A menu is simply a column of buttons that can be activated by clicking on them. When they are clicked on, a popup menu may be displayed showing further options. These options can also be popup menus in themselves, and can contain a further set of options, etc.

To demonstrate how we will incorporate a menu into our C++ project via a resource file, open Microsoft Visual C++ 6 and create a new empty Win32 project as described in article 1. Name the project “MenuApp”.

Right click on the “MenuApp classes” node under the ClassView tab, and add a new class. Name the new class “menu”, as shown below:

Our new class, containing menu.cpp and menu.h

Click on the FileView tab and expand all of the nodes. Double click on menu.h and then menu.cpp and delete all of the code in the right hand window. To re-create our basic application skeleton, enter the following code into menu.h:

class CMainWin : public CFrameWnd

{

public:

CMainWin();

DECLARE_MESSAGE_MAP()

};

class CApp : public CWinApp

{

public:

BOOL InitInstance();

};


Next, double-click on menu.cpp and enter the following code:

#include <afxwin.h>

#include "menu.h"

CMainWin::CMainWin()

{

Create(NULL, "My Test Menu Application");

}

BOOL CApp::InitInstance()

{

m_pMainWnd = new CMainWin;

m_pMainWnd->ShowWindow(m_nCmdShow);

m_pMainWnd->UpdateWindow();

return TRUE;

}

BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)

END_MESSAGE_MAP()

CApp App;


I wont describe the code above because it was covered in part one of this series.
blog comments powered by Disqus
C++ ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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