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