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 application logic.
Creating a Window using MFC
The first task to be done in any application is to create a window for an application. MFC provides two important classes viz.
CFrameWnd, which can be used for creating a window and 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
CCmdTarget is created with the capability to handle Windows messages, which is referred to as Message Maps. If you wonder what this Message map means, "Message maps are macros which take care of the event handling".
CWinApp class has an important over-ridable function,
InitInstance, which handles the window creations. The next important one is a data member,
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.
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. Just choose Empty project.
After the project is created, click on Menu -->Project --> Add to Project -->New and select a .cpp file and give a name to it.
Copy and paste the code below:
class MFC_Tutorial_Window :public CFrameWnd
Create(NULL,"MFC Tutorial Part 1 CoderSource Window");
class MyApp :public CWinApp
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
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.
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.
This tutorial is a very simple illustration of how to create a window using MFC.
Note: This article an also be found at Codersource MFC Tutorial 1. The codersource website belongs to this author.