5.2 Work from a Template

This section explains the second step in developing Phone Book from Hello: borrow most of Hello's code for Phone Book, thus using Hello as a template to get you started.

·To copy and modify the Hello files:

1.Make a new directory in your \MFC\SAMPLES\TUTORIAL directory.

You'll use this directory to put together your own files for the tutorial. You might call the directory CHAPTER5, for instance.

2.Copy HELLO.H with the name VIEW.H in your new directory and change the “Hello” filename in the header comment of VIEW.H to “View.”

3.Add or modify the header comments in VIEW.H, replacing references to “Hello” with “View.”

4.Change all other references to “Hello” in VIEW.H to “View.”

5.Add the following lines to VIEW.H:

#ifndef __VIEW_H__

#define __VIEW_H__

6.Copy HELLO.CPP to VIEW.CPP. In VIEW.CPP, do the following:

A.Replace the #include directives copied from Hello with the following:

#include <afxwin>
#include "resource.h"
#include "database.h"
#include "view.h"

extern "C"
{
#include <commdlg.h>
}

#define SIZESTRING 256
#define SIZENAME 30
#define SIZEPHONE 26
#define PAGESIZE 8

// A simple way to reduce size of C run times
// Disables the use of getenv and argv/argc
extern "C" void _setargv() {}
extern "C" void _setenvp() {}

These declarations specify include files, define preprocessor constants, and invoke a simple technique to reduce the size of the run-time libraries that are incorporated into the program. The extern “C” directives specify that C code is to be used in a C++ program. The #include statement for file COMMDLG.H causes inclusion of code for the common open, save, and print dialog boxes, which are available in COMMDLG.DLL for Windows, versions 3.0 and 3.1.

B.Delete the contents of the CMainWindow constructor (but leave the function itself). You'll replace the constructor code later.

C.Remove the present contents of the OnPaint member function of class CMainWindow (but leave the function itself). You'll replace the contents later. Also edit the header comment for OnPaint to remove references to Hello.

D.Replace all references to Hello in VIEW.CPP with “View.”

When you complete the steps above, your files will be ready to receive additional code for the Phone Book program. Notice that nearly all of the code copied from Hello to the VIEW files is still perfectly usable, and it gives you a framework into which you can add your Phone Book code.

To continue the tutorial, see the next section.