Friday, January 06, 2006

An example MFC program

//hello.cpp

#include

// Declare the application class
class CHelloApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

// Create an instance of the application class
CHelloApp HelloApp;

// Declare the main window class
class CHelloWindow : public CFrameWnd
{
CStatic* cs;
public:
CHelloWindow();
};

// The InitInstance function is called each
// time the application first executes.
BOOL CHelloApp::InitInstance()
{
m_pMainWnd = new CHelloWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}

// The constructor for the window class
CHelloWindow::CHelloWindow()
{
// Create the window itself
Create(NULL,
"Hello World!",
WS_OVERLAPPEDWINDOW,
CRect(0,0,200,200));
// Create a static label
cs = new CStatic();
cs->Create("hello world",
WS_CHILD|WS_VISIBLE|SS_CENTER,
CRect(50,80,150,150),
this);
}
The program is doing three things:

1. Creating the application object
Every program has a single application object that handles the initialization details of Windows and MFC

2. The application then creates a single window on the screen to act as the main application window

3. Inside the window, the application creates a single static text label containing the words 'hello world"


1. Declare the application class
2. Create an instance of the application class
3. Declare the main window class
4. InitInstance function is called each time the application first executes
5. Constructor for the window class
1. Create the window
2. Create the static label

0 Comments:

Post a Comment

<< Home