MFC Tutorial Part 1

MFC provides really nice classes for doing windows programming. It has taken away all the pains of SDK, where a programmer is expected to handle everything in the world except writing for application logic.It used to be such a big pain, that people hated SDK so much and lots of them shifted to VB development.

This series of MFC Tutorial articles, will try to cover some of the basics for a good start in MFC Programming. The first task to be done in any application is to create a window for an application. MFC provides two important classes viz. CWinApp & CFrameWnd, which can be used for creating a window & the application. Both these classes have their own message handling mechanisms, screen-drawing functions etc.,
To be precise, CWinApp provides the application level functionalities and CFrameWnd provides the functionalities related to GUI.

All these classes are derived from CCmdTarget which in turn is derived from CObject. CCmdTarget is created with the capability to handle windows messages, which is referred as Message Maps. If you wonder what this Message map means, “Message maps are macros which take care of the event handling”. This topic will be dealt in the next part of our MFC Tutorial (Part 2).

The CWinApp class has an important over-ridable function InitInstance which handles the Window creations. The next important one is a data member, m_pMainWnd (of CWinApp) which holds the pointer to the window.

Let’s write an example and try to understand it. Follow these steps to create an application with minimal usage wizard. The wizard will be used just to create a simple workspace for this MFC Tutorial.

Step1:
Create a new project of type Win32 application. In the second screen of the wizard, select the first option. Do not allow the wizard to add any files.

Step 2:
After the project is created, click on Menu –>Project –> Add to Project –>New and select a .cpp file and give a name to it.

Step 3:
Copy and paste the code below.

//MFC1.CPP - MFC Tutorial Part 1 from codersource.net

#include 

class MFC_Tutorial_Window :public CFrameWnd
{
public:
MFC_Tutorial_Window()
{
Create(NULL,"MFC Tutorial Part 1 CoderSource Window");
}
};

class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
public:
BOOL InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(1);
return 1;
}
};

MyApp theApp;

//End of program MFC Tutorial Part 1

Step 4:
Now compile the application. It will generate linker errors. The reason is because the MFC libraries are not yet linked. Do the last step of this MFC Tutorial.

Step 5:
Select Menu –>Project –> Settings and General tab. On the first combo box “Microsoft Foundation classes” and select “Use MFC in shared dll”. Now compile and run the program.

This MFC Tutorial example creates a simple Window with a title called “MFC Tutorial Part 1 CoderSource Window”. The window has no contents, menu or any other controls.

The next part of this MFC Tutorial series will handle the left out topics one by one.