This section explains the first step in writing Hello: the creation of the application object. You'll create parts of two files in this section: HELLO.H and HELLO.CPP.
This process consists of two steps:
1.Derive an application class.
2.Write a Windows function.
·To derive an application class:
1.Create a HELLO.H interface file.
A.Add the following preprocessor directives:
#ifndef __HELLO_H__
#define __HELLO_H__
These directives are similar to the ones you used in PERSON.H in the previous chapter. They prevent multiple inclusion of any code in the HELLO.H file.
B.Add the following class declaration to your HELLO.H file:
class CTheApp : public CWinApp
{
public:
// An override of InitInstance
BOOL InitInstance();
};
You derive Hello's application class, CTheApp, from the Microsoft Foundation class CWinApp. The name of the derived class is your choice. For Hello, it's called CTheApp.
Class CTheApp inherits member variables and member functions from its base class but overrides the InitInstance member function of class CWinApp. See the discussion under “Discussion: Hello's Application Class” on page 87.
2.Add the following preprocessor directive to the bottom of file HELLO.H:
#endif // __HELLO_H__
As you add more declarations to the file later, keep this directive as the last item in the file.
Create a HELLO.CPP implementation file. A.Add the following preprocessor directives at the top:
#include <afxwin.h>
#include "resource.h"
#include "hello.h"
When you program with the Microsoft Foundation Class Library, always include the file AFXWIN.H.
B.Add the following variable declaration to HELLO.CPP below the #include directives:
CTheApp theApp;
This declares a variable of type CTheApp in order to construct the program's one and only application object. A suitable name for this variable is theApp.
After the application object is constructed from this variable, its member functions are called to initialize the application and to create a window object. See the following discussion.
·To create a window object:
1.Write your overriding InitInstance member function, which is where you create a main window object.
2.Add the following function definition for InitInstance to the end of your HELLO.CPP file:
BOOL CTheApp::InitInstance()
{
// Construct a window object in the heap
m_pMainWnd = new CMainWindow();
// Show the window
m_pMainWnd -> ShowWindow( m_nCmdShow );
// Paint the window
m_pMainWnd -> UpdateWindow();
return TRUE;
};
Since the HELLO.CPP file will eventually contain functions belonging to two different classes, set up a section for each class.
InitInstance is an appropriate place to construct a main window object,
which will be responsible for putting a window on the screen. Note that
putting a window on the screen is a three-step process: construct the main window object (which creates a window), call two of its member functions
to make the new window visible, and cause its client area to be painted. This process is explained further in the following discussion and in “Paint the Window” on page 101.
To continue with the tutorial, see “Put a Window on the Screen” on page 90. For more information about the code you just added, see the following discussion.