Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / MFC
Article

MFC Tutorial - Creating a window with two classes

Rate me:
Please Sign up or sign in to vote.
3.12/5 (20 votes)
8 Mar 20042 min read 157K   40   15
This article explains how to create a window in MFC without using the App Wizard. It uses only two classes, CFrameWnd and CWinApp, for creating the window.

Introduction

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. CWinApp and 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 CObject. 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".

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. Just choose Empty project.

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

#include <afxwin.h>

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(SW_SHOW);
       return TRUE;
   }
};

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.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInitInstance virtual Pin
Toby Chaloner27-Oct-18 3:45
Toby Chaloner27-Oct-18 3:45 
GeneralMy vote of 4 Pin
venkat.yva13-Sep-13 23:43
venkat.yva13-Sep-13 23:43 
GeneralMFC1 code produces error. Pin
gregoryvaughn12-May-10 10:09
gregoryvaughn12-May-10 10:09 
AnswerRe: MFC1 code produces error. Pin
jlc32123-Nov-10 5:22
jlc32123-Nov-10 5:22 
Questionentry point must be defined... Pin
weagle0830-Apr-10 10:45
weagle0830-Apr-10 10:45 
AnswerRe: entry point must be defined... Pin
gregoryvaughn12-May-10 10:13
gregoryvaughn12-May-10 10:13 
AnswerRe: entry point must be defined... Pin
gregoryvaughn12-May-10 10:14
gregoryvaughn12-May-10 10:14 
GeneralClass Pin
hosseinzpp1-Mar-10 23:58
hosseinzpp1-Mar-10 23:58 
GeneralThis tutorial is very straigtforward and excellent Pin
jamaican_prince16-May-07 21:36
jamaican_prince16-May-07 21:36 
GeneralThis article sucks Pin
steveusa39200011-Dec-06 11:24
steveusa39200011-Dec-06 11:24 
GeneralRe: This article sucks PinPopular
Jordan Walters4-Jan-07 10:50
Jordan Walters4-Jan-07 10:50 
QuestionSDL window in MFC Dialog Pin
Manjunath S1-Jun-06 2:02
Manjunath S1-Jun-06 2:02 
How To Display the SDL window in MFC dialog ??

Manjunath S
GESL
Bangalore
GeneralDoh! PinPopular
Larry Antram9-Mar-04 17:49
Larry Antram9-Mar-04 17:49 
GeneralRe: Doh! Pin
Muthukumar9-Mar-04 18:41
Muthukumar9-Mar-04 18:41 
GeneralRe: Doh! Pin
jlc32123-Nov-10 5:11
jlc32123-Nov-10 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.