Home arrow C++ arrow Page 4 - 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 / 78
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 - Adding the code to MyClass.cpp
(Page 4 of 7 )

Now that we have declared the two main classes of our application, we will want to create the implementation code for their constructors and functions. Double click on the MyClass.cpp file. Start by entering the follow code in its code window:

#include <afxwin.h>

#include "MyClass.h"

These are just simple include directives which reference our header file and another special MFC file, afxwin.h. afxwin.h contains all of the declarations and references for access to the MFC classes, including the CframeWnd and CwinApp classes from which we have derived our classes in MyClass.h. All MFC applications require the afxwin.h header file to be included. For now, just remember that the afxwin.h file contains all of the MFC classes which create windows and send and respond to messages.



Create(NULL, "My MFC Application");


This is the implementation code for the constructor of the CMainWin class, which is responsible for creating an instance of our application.

The Create() function is available to our app through the CframeWnd class which is what our CMainWin class is derived from. The create function takes 8 parameters, but we only need the first two. The second parameter, “My MFC Application” will simply set the title bar text in our application. That’s all we need for our CMainWin constructor. Let’s move on to the InitInstance() function of our CApp class.

BOOL CApp::InitInstance()




m_pMainWnd = new CMainWin;



return TRUE;




return FALSE;



This might be the hardest function to get your head around. It’s the InitInstance function, takes no parameters and returns a BOOL value. In MFC, all of the simple data types such as bool, unsigned int and char have been typedef’d into upper case versions such as BOOL, UINT and LPCTSTR. This is to distinguish regular C++ code from MFC code.

A try catch block surrounds the entire function to grab any errors that the code in the try block might throw. The try block starts by setting the member pointer m_pMainWnd to a new instance of our CMainWin class. The m_pMainWnd variable is a pointer to a CWnd* object, which tells windows where the entry point into our application is.

Next, we call the show window member function of the m_pMainWnd object, which tells windows to display our dialog window. The paramater, m_nCmdShow, is an MFC defined value. Lastly, we call the UpdateWindow() function which tells windows to redraw our main dialog window. If all goes well, the function returns TRUE (Once again, TRUE is an MFC typedef’d value of the C++ variable true). A return value of TRUE is required to load our app successfully.

Now that we have all of the class declarations and implementation code out of the way, we will talk more about messages. Remember how we have the DECLARE_MESSAGE_MAP() macro declared in our CMainWin class in MyClass.h? Well, this macro tells windows that the CMainWin class (our main class) will send and respond to windows messages respectively.

To actually tell windows which messages our application can send and receive, we need to use the BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() macros in our MyClass.cpp file. Add the following code to MyClass.cpp. I will explain it in a minute:



These two simple lines are MFC defined macros. They represent several MFC functions (you can see this for yourself by finding the afxwin.h file on your system and looking at it in notepad) and take two parameters: The name of the Window we are working with and it’s base class. We are working with our main window, CMainWin, which is derived from CframeWnd. Between these two macros, we will put the macros for the messages that our application will respond to. But first, let’s create an instance of our app and make sure our application runs.

Add the following code at the end of the MyClass.cpp file:

CApp App;

This one simple line will create an instance of our CApp class, which in turn will bring everything together to display our app. We’ve created the bulk of our code, and can now see what our application looks like.
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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials