Home arrow C++ arrow Page 3 - Using MFC in C++ Part 1: A Basic Application Skeleton

Using MFC in C++ Part 1: A Basic Application Skeleton

Microsoft Foundation Classes allow C++ developers to write windows apps easier and faster. In this article, Mitch starts from scratch and digs deep into the MFC toolbox to provide you with all of the code and details to create a simple MFC based Win32 app.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 79
November 26, 2001
  1. · Using MFC in C++ Part 1: A Basic Application Skeleton
  2. · Building the framework
  3. · Creating the framework classes
  4. · Adding the code to MyClass.cpp
  5. · Making sure our application runs
  6. · Responding to windows messages
  7. · Conclusion

print this article

Using MFC in C++ Part 1: A Basic Application Skeleton - Creating the framework classes
(Page 3 of 7 )

You will notice that you still have an empty workspace and that your project contains no classes. To remedy this, right click on the “MFCSample Classes” none under the ClassView tab in the Workspace window and click on the New Class… option.

This will show the “New Class” dialog. In the Name field, type in MyClass and click on the OK button. This will add a new class to your application, which will contain an implementation file (MyClass.cpp) and a header file (MyClass.h). Click on the FileView tab of the workspace dialog and expand all nodes.

The Workspace Dialog Containing Our “MyClass” Clas

When you add a new class to any application in Visual C++, it likes to “junk up” each implementation and header file with the usual #if !defined … #endif directives. Double click on MyClass.cpp and delete all of the code in the main window. Do the same for the MyClass.h file. We don’t need this code!

Now, in the code window for MyClass.h, type in the following:

class CMainWin : public CFrameWnd






class CApp : public CWinApp



BOOL InitInstance();


As you can probably guess, we’ve just created two classes which are derived from two other classes: CframeWnd and CwinApp. These are MFC classes that are defined in afxwin.h (More on this file soon). The CApp class creates an instance of an application, while the CMainWin class creates an instance of a windowed dialog. That’s all you need to know about them for now.

In the CMainWin class, we have publicly declared the constructor and the DECLARE_MESSAGE_MAP() macro. This is defined internally by MFC and will allow our application to send and accept messages to and from windows.

All C++ applications are created around the simple concept of sending and posting messages. Let’s say that, for example, you click on a button in an application. Internally, windows will recognize this click because your application has sent windows a unique message identifier letting it know that the button was clicked. Windows responds by sending a message to your application letting it know that a certain button was clicked. Your application “catches” the message and calls a function to handle the click. If you’ve had some exposure to Visual Basic, then it’s similar to writing code in the Command1_Click() sub routine for the click of a button.

Not all messages that windows sends to your application have to be responded to. If your app doesn’t respond to a message, then windows will create a default implementation for it. This is an important point, which you should try and remember.

The last class definition in our MyClass.h header file, CApp, is derived from the MFC class CwinApp. The InitInstance() function is publicly overwritten and will contain the code which will be requested when the application is initially instantiated.
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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials