Click here to Skip to main content
Click here to Skip to main content

MFC Tutorial - Creating a window with two classes

By , 8 Mar 2004
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMFC1 code produces error.membergregoryvaughn12 May '10 - 10:09 
This code produces the following error:
 
WINVER not defined. Defaulting to 0x0600 (Windows Vista)
c:\users\tech\documents\visual studio 2008\projects\project1\mfc2\mfc2.cpp(10) : error C2664: 'CFrameWnd::Create' : cannot convert parameter 2 from 'const char [39]' to 'LPCTSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Users\Tech\Documents\Visual Studio 2008\Projects\Project1\MFC2\Debug\BuildLog.htm"
MFC2 - 1 error(s), 0 warning(s)
 
I understand what is being referred to but I am hard pressed to effect a remedy.
Gregory A. Vaughn Sr.

AnswerRe: MFC1 code produces error.memberjlc32123 Nov '10 - 5:22 
Bracket the second Create() argument with TEXT ( )
viz a viz:
Create( NULL, TEXT("MFC Tutorial Part 1 CoderSource Window") );
Questionentry point must be defined...memberweagle0830 Apr '10 - 10:45 
I tried this and got LNK error "entry point must be defined"... isn't this defined in one of the afxwin.h classes that get included?
 
What am i doing wrong?
I'm using VS 2005 and the code exactly as written above.
weagle08
WAR EAGLE!

AnswerRe: entry point must be defined...membergregoryvaughn12 May '10 - 10:13 
I got that error when I did't start with an empty [project. It seemed to pickup erroneous info from paarams not needed.
Gregory A. Vaughn Sr.

AnswerRe: entry point must be defined...membergregoryvaughn12 May '10 - 10:14 
I got that error when I didn't start with an empty project. It seemed to pickup erroneous info from paarams not needed.
Gregory A. Vaughn Sr.

GeneralClassmemberhosseinzpp1 Mar '10 - 23:58 
hello
How do I creat object from CDialog class and covert to CStatic class.
hosseinjavidian@gmail.com
thanks.
GeneralThis tutorial is very straigtforward and excellentmemberjamaican_prince16 May '07 - 21:36 
Due to the bloat in windows and the hidden things it does for you, this tutorial is actually very good in letting any starting MFC programmer understand the basics of MFC Smile | :)
 

 
Simple

GeneralThis article sucksmembersteveusa39200011 Dec '06 - 11:24 
This article sucks. No demo code, just a bunch of #*!*# that means nothing
GeneralRe: This article sucksmemberJordan Walters4 Jan '07 - 10:50 
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 DialogmemberManjunath S1 Jun '06 - 2:02 
How To Display the SDL window in MFC dialog ??
 
Manjunath S
GESL
Bangalore

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

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