Home Other MFC Fundamentals and Architecture

MFC Fundamentals and Architecture

Chapter 2: MFC Fundamentals and Architecture

The Microsoft Foundation Class (MFC) Library and Visual C++ provide an environment that you can use to easily create a wide variety of applications. This chapter discusses the various class types and primary architectures that are used in most applications.

Objectives

After completing this chapter, you will be able to:

  • List the major categories of MFC classes and the primary classes derived from each category.
  • Describe the relationship of the foundation class CObject to the other MFC classes.
  • Explain the key features CObject contributes to the classes derived from it.
  • Define the main base classes used in a typical MFC application.
  • Describe the document/view architecture and how this architecture applies to MFC.
  • Describe the benefits and costs of document/view, non-document/view, and dialog-based application architectures.
  • Describe how MFC maps messages to functions.

Introduction to MFC

The MFC Library is a collection of C++ classes and an application framework designed primarily for creating Microsoft Windows-based applications. This collection of classes extends the C++ language to include most of the basic structural elements used to create Windows programs. The application framework defines the structure of an application and handles many routine tasks for the application.

This section describes the relationship between MFC and Windows technology and explains some key advantages that MFC and Visual C++ offer developers.

This section includes the following topics:

MFC and the Win32 API

The MFC Library is built on top of the Win32 application programming interface (API). This API is a set of functions exposed by the operating system for use by applications. Through MFC, base classes are exposed that represent common objects in the Windows operating system, such as windows and menus.

MFC does not encapsulate or wrap, the entire API, just the main structural components and components that are commonly used. Because MFC is written in C++, MFC developers can easily use the Win32 API to make native calls to the operating system.

To see an illustration that shows the relationship between MFC, the Windows Base Operating Services, and the Windows Operating System Extensions, click this icon.

MFC version 1.0 was little more than a thin wrapper for the Win32 API. However, each subsequent version of MFC provides significantly more features and functionality. MFC incorporates many names and constructions familiar to developers who have used the Microsoft Windows Software Development Kit (SDK). For example, MFC provides a member function named CWnd::InvalidateRect that corresponds to the Win32 function InvalidateRec.

Wrapper Classes for Operating System Extensions

MFC encapsulates important Microsoft Windows operating system extensions, including COM, ActiveX, ODBC, and Windows Internet APIs. The encapsulation is not complete. However, combining MFC and native API usage is usually simple.

Benefits of Using Visual C++

Visual C++ provides a productive visual development environment that gives you the tools you need to create a wide variety of applications, from client/server solutions to high-performance multimedia applications.

Visual C++ supports the latest in Windows operating system-based technology, including the Windows 95 operating system. Visual C++ offers full support for the common controls in the Windows 95 and Windows NT operating systems, as well as support for the Windows 95 Explorer shell. Most of the new Windows 95-based common controls are fully supported by Visual C++, and you can access them through MFC and the Win32 API.

Some other features that make Visual C++ an excellent choice for developing solutions are:

  • Code reusability.
  • Application wizards for MFC applications, DLLs, ActiveX controls, Active Template Library (ATL) projects, ATL Component Object Model (COM) objects, and ISAPI extensions.
  • The integrated development environment of the Developer Studio.
  • Components and Controls Gallery, a place to store and access reusable components and controls.
  • Support for the MFC Library.
  • Portability and cross-platform support.
  • Availability in Chinese, English, German, French, Italian, Korean, Japanese, and Spanish.
  • Support for the latest C++ features.
  • Benefits of Using MFC

The Microsoft Foundation Class (MFC) Library enables you to build on the work of expert Windows developers. MFC shortens development time, makes the code more portable, provides support without reducing programming freedom and flexibility, and provides easy access to hard-to-program user-interface elements and technologies such as ActiveX, OLE, and Internet programming. Furthermore, MFC simplifies database programming through data access objects (DAO) and Open Database Connectivity (ODBC) and simplifies network programming through Windows Sockets. MFC makes it easy to program such features as property sheets (tab dialog boxes), print preview and floating, customizable toolbars.

By using MFC, you can add many features to your applications in an easy, object-oriented way. In MFC version 3.0, you could add ToolTips to your application by adding only three lines of code. With MFC 4.0, a similar amount of code enables ActiveX controls in your application. Now with MFC version 4.2, Visual C++ 5.0, the Gallery, and the wizards, you can add the Active Template Library (ATL) components, Internet classes, and synchronization for multithreaded programs.

MFC offers the experienced C++ developer many advantages when developing full-featured Windows applications, front-end database applications, ActiveX controls and components, and Internet applications, such as:

  • An application framework on which you can build a Windows application
  • Compatibility with previous MFC versions and the new C++ classes
  • The largest base of reusable C++ source code in the industry
  • Integration with Visual C++
  • Flexible, fast database access methods using DAO or ODBC
  • Support for ActiveX and Internet technologies
  • Support for popular data-binding technologies
  • Support for Windows messaging API
  • Support for multithreading

The MFC Class Hierarchy

The first step on the road to becoming an MFC developer is becoming familiar with the classes. This section gives you an overview of the kind of infrastructure MFC provides, including the classes and class hierarchy.

MFC provides nearly 250 classes designed to serve a wide range of programming needs. The first step in programming with MFC is to become familiar with these classes and learn how they are related to one another in the MFC hierarchy. Some classes are used directly, while others serve as base classes for the classes that you create.

When learning about the MFC classes, it is helpful to organize them into the following categories, which are the topics included in this section:

  • CObject-Derived Classes
  • Application Architecture Classes
  • User-Interface Classes
  • General-Purpose Classes
  • ActiveX Classes
  • Database Classes
  • Internet Classes
  • Global Afx Functions

These categories of MFC classes are not mutually exclusive. The majority of MFC classes are derived, either directly or indirectly, from CObject.

CObject-Derived Classes

CObject is the base class for most of MFC and provides the foundation for serialization (loading and saving data to a file), run-time class information, diagnostic and debugging support (validations and dumps), and compatibility with collection classes.

CObject Hierarchy

To see an illustration that shows the relationship of CObject to the other classes in the MFC class hierarchy, click this icon.

The classes in MFC that are derived from CObject provide most of the structure and functionality of an MFC application. The following table lists some of the most important classes and describes the tasks they perform.

Category

Base class

Description

Command targets
CCmdTarget
For classes that are capable of handling the user’s requests.

Applications
CWinApp
Represents the core of the application.

Documents
CDocument
Contains an application’s data set.

Windows
CWnd
For graphical user interface (GUI) objects that are capable of handling general Windows messages.

Frames
CFrameWnd
For an application’s primary window frame.

Views
CView
Displays data and interacts with the document object.

In addition, there are many CObject-derived classes for other aspects of a Win32-based application, such as menus, graphics, file services, and so on.

Support for CObject-Derived Classes

CObject provides three basic types of support to classes that inherit from it, as shown in this table.

Type of support
Description

Serialization
In this context, serialization is the process of writing or reading an object to or from a persistent storage medium, such as a disk file.

Run-time class information
Because it is derived from CRuntimeClass, objects derived from CObject contain information about their origins that are accessible at runtime. This run-time type-checking feature gives functions the ability to react based on the type of object that is acted on.

Diagnostic output
To aid in the development of an MFC application, CObject provides a number of diagnostic or debug output functions. Overriding these functions provides information about the execution of your program that is especially useful when program behavior is not as expected.

Classes Not Derived from CObject

MFC also contains a set of classes that are not derived from CObject. These classes are useful in situations where the overhead associated with CObject and run-time type identification are unnecessary. These classes fall into three general categories, as shown in the following table.

Category
Examples

General utility classes for general programming
CString, CTime, CTimeSpan, CRect, CPoint, CSize

MFC support classes to assist the architecture
CArchive, CDumpContext, CRuntimeClass, CFileStatus, CMemoryState

Templatized versions of the collection classes that take a user-defined pointer
CTypedPointerArray

Application Architecture Classes

The application architecture classes represent the basic architectural elements of an application and include CWinApp, which represents the application itself. To see an illustration that shows the architecture classes in the MFC object hierarchy, click this icon.

Some of the architectural classes include CWinApp, CDocument, CCmdTarget, and CWinThread. These classes are the first to be instantiated when the application starts, and they have significant responsibility.

The following table describes these four classes in more detail.

Class name
Description

CWinApp
This class represents the application itself. All MFC applications derive a class from CWinApp.Depending on the type of framework chosen, the application object is responsible for:

Initializing the application.

Building the document template structures.
Providing the message loop with retrieved messages from the message queue and dispatching these messages to the appropriate location.
Cleaning up the application on exit.

CDocument
This is the base class for documents in applications that use document/view architecture. A document is simply an abstract representation of a program’s data. A major architectural task for the developer is to determine how data is to be stored in the document, and how and to what extent the document will provide access to that data.

CCmdTarget
This is the foundation class for message map architecture in MFC. The capabilities that a class inherits from CCmdTarget enable it to serve as a target for a special class of messages, known as command messages. These messages are generated when the user chooses a command from a menu.

CWinThread
This class encapsulates the threading capabilities of the operating system. Member functions enable MFC applications to create and manipulate threads.

User-Interface Classes

Typically, the user-interface classes encompass the elements of a Windows-based application that are visible to the user. These include windows, dialog boxes, menus, and controls. The user-interface classes also encapsulate the Windows device context and graphics device interface (GDI) drawing objects.

To see an illustration that shows the user-interface classes within the MFC object hierarchy, click this icon.

Some of the user-interface classes include CWnd, CView, CGdiObject, and CMenu.

CWnd

CWnd is the base class for all MFC windows and defines the basic functionality of a window and a window’s default response to most messages. CWnd is sometimes used directly to derive classes, but more often, classes are derived from the other built-in classes that are derived from CWnd. The following table shows some of the classes that are derived from CWnd.

Class and Description

CFrameWnd
Models the behavior of a single document interface (SDI) frame window.

CControlBar
Serves as the base class for toolbars, status bars, and other controls.

CDialog
Encapsulates the functionality of dialog boxes.

CButton, CListBox, CScrollBar, and so forth
Corresponds to controls such as buttons, list boxes, and scroll bars.

CView

Most applications provide the user with one or more views of the underlying data. For example, Microsoft Excel provides a worksheet view and a graphic view of the same data. The CView class, derived from CWnd, is the base class that provides visible representation in a document/view application.

CGdiObject

The graphics device interface (GDI) portion of Windows is designed to provide applications with a device-independent and feature-rich set of drawing capabilities. The GDI contains a number of “objects” that are used to display output, such as brushes, pens, and fonts. CGdiObject is a wrapper class that provides MFC applications with the ability to create and use these objects.

CMenu

Menus are one of the main methods by which users interact with applications. An application’s menu items not only give the user the ability to invoke an action, but they also provide information to the user about the state of the application and what options are currently available.

The CMenu class provides an object-oriented interface to menus. Through the CMenu class interface, the application can dynamically control its menus at run time.

®  General-Purpose Classes

MFC includes a number of general-purpose classes that do not encapsulate the Win32 API. These classes represent simple data types such as points and rectangles and more complex data types such as strings.

To see an illustration that shows the general-purpose classes within the MFC CObject hierarchy, click this icon.

The following table lists some commonly used general-purposes classes and describes the tasks they perform.

Class and  Task

CFile
File I/O

CString
Managing string variables

CException
Exception handling

CByteArray, CIntArray, CStringArray, CStringList, CObList
Working with data structures such as arrays and linked lists

CPoint, CSize, CRect, CTime, CTimeSpan
Miscellaneous

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version