Saturday, February 04, 2006

MFC Application


Creating Simple MFC Application
without using wizards
I assume reader familiar with C++.
Follow these steps to create an simple

MFC application with a Frame Window.

Create a file with cpp extension.
Create a class which is derived from
CFrameWnd with Constructor and destructor


class CMyFrame
: public CFrameWnd
{
public:
CMyFrame()
{
}
virtual ~CMyFrame()
{
}

};


Create a class which derived from CWinApp and
override InitiInstance function we will create
Frame Window using the
CMyFrame.


class CMyApp: public CWinApp
{
public:
virtual BOOL InitInstance()
{
CMyFrame* myFrame = new CMyFrame();
m_pMainWnd = myFrame;
//Assign myFrame pointer to 'm_pMainWnd'
//which is member variable of CWinApp
myFrame->Create(NULL,"My Frame Window");
myFrame->ShowWindow(SW_SHOW);
myFrame->UpdateWindow();

return TRUE;

}

};



Last We will create CMyApp's instance


CMyApp myApp;

Include afxwin.h at top of
file which is MFC header file

#include

Put all code together

#include

class CMyFrame : public CFrameWnd
{
public:
CMyFrame()
{
}
virtual ~CMyFrame()
{
}

};

class CMyApp: public CWinApp
{
public:
virtual BOOL InitInstance()
{
CMyFrame* myFrame = new CMyFrame();
m_pMainWnd = myFrame;
//Assign myFrame pointer to 'm_pMainWnd'
//which is member variable of CWinApp
myFrame->Create(NULL,"My Frame Window");
myFrame->ShowWindow(SW_SHOW);
myFrame->UpdateWindow();

return TRUE;

}

};

CMyApp myApp;

Copy and paste this code to our cpp file and save.

Step1. Create an empty Win32 Application
and add this file to Project.
Step2. Enable MFC using project settings.
Step3. Build and Execute.

It will open Simple Frame window with System Menu.

0 Comments:

Post a Comment

<< Home