Click here to Skip to main content
Licence 
First Posted 8 Mar 2004
Views 78,311
Bookmarked 31 times

MFC Tutorial - Creating a window with two classes

By | 8 Mar 2004 | Article
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

About the Author

Muthukumar

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMFC1 code produces error. Pinmembergregoryvaughn10:09 12 May '10  
AnswerRe: MFC1 code produces error. Pinmemberjlc3215:22 23 Nov '10  
Questionentry point must be defined... Pinmemberweagle0810:45 30 Apr '10  
AnswerRe: entry point must be defined... Pinmembergregoryvaughn10:13 12 May '10  
AnswerRe: entry point must be defined... Pinmembergregoryvaughn10:14 12 May '10  
GeneralClass Pinmemberhosseinzpp23:58 1 Mar '10  
GeneralThis tutorial is very straigtforward and excellent Pinmemberjamaican_prince21:36 16 May '07  
GeneralThis article sucks Pinmembersteveusa39200011:24 11 Dec '06  
GeneralRe: This article sucks PinmemberJordan Walters10:50 4 Jan '07  
In defense of the original author, the code snippet is so small that a monkey could type it in in a minute or two. Why do you need example code? Are you not up to the task?
 
And to say that the code means ######-all just illustrates either your lack of understanding of some quite simple concepts, or your lack of appreciation when the author tries to demonstrate to beginners how to do simple things. Ok, he may not follow standard microsoft MFC paradigms and rigorous software engineering principles - but are you not able to see beyond this?
 
Shame on you!
QuestionSDL window in MFC Dialog PinmemberManjunath S2:02 1 Jun '06  
GeneralDoh! PinmemberLarry Antram17:49 9 Mar '04  
GeneralRe: Doh! PinmemberMuthukumar18:41 9 Mar '04  
GeneralRe: Doh! Pinmemberjlc3215:11 23 Nov '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 9 Mar 2004
Article Copyright 2004 by Muthukumar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid