Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 2: Introduction to MFC


AssertValid(): This method is used to check your class. Because an inherited class is
usually meant to provide new functionality based on the parent class, when you create a
class based on CObject, you should provide a checking process of your variables. In this
case you should provide your own implementation of the AssertValid() method. You can
use it for example to check that an unsigned int variable is always positive.

The objects of an applications send messages to the operating system to specify what they
want. The MFC provides a special class to manage these many messages. The class is
called CCmdTarget. We will come back to it when dealing with messages.

2.1.3 The Basic Application............................................................................


To create a program, also called an application, you derive a class from the MFC's
CWinApp. CWinApp stands for Class For A Windows Application.

Here is an example of deriving a class from CWinApp:

struct CSimpleApp : public CWinApp
{
};

Because the CWinApp class is defined in the AFXWIN.H header file, make sure you
include that file where CWinApp is being used. To make your application class available
and accessible to the objects of your application, you must declare a global variable from
it and there must be only one variable of your application:

struct CSimpleApp : public CWinApp
{
};

CSimpleApp theApp;

The CWinApp class is derived from the CWinThread class. The CWinApp class
provides all the basic functionality that an application needs. It is equipped with a method
called InitInstance(). To create an application, you must override this method in your
own class. Its syntax is:

virtual BOOL InitInstance();

This method is used to create an application. If it succeeds, it returns TRUE or a non-
zero value. If the application could not be created, it returns FALSE or 0. Here is an
example of implementing it:

struct CSimpleApp : public CWinApp
{
BOOL InitInstance() { return TRUE; }
};

Based on your knowledge of C++, keep in mind that the method could also have been
implemented as:

struct CSimpleApp : public CWinApp
{
BOOL InitInstance()
{
Free download pdf