Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

Visual C++ is probably the most robust language available to date. No one can deny the fact that Visual C++ is a language meant for expert programmers

and developers, those who provide the most powerful and efficient solutions. True

Y ues to provide the same benefits as before:Lpower, flexibility, performance, and

to these words, Visual C++ .NET, the next in the series after VC++ 6.0, contin-

control. Visual C++ .NET, which is Fone of the languages supported by the .NET Framework, is probably the onlyMlanguage that has undergone few changes, when compared to Visual Basic, which has undergone some radical changes.

So, what does this mean toAyou? Just that the fundamental concepts of programming in Visual C++ areEstill the same — yes, the good old MFC, Microsoft Foundation Classes, is stillTthere, but with more enhancements. This chapter addresses the basics of programming using MFC. Programming with managed extensions (VC++ .NET’s feature that prepares VC applications for the .NET Framework) and enhancements to COM programming are addressed in Chapters 10 to 13.

If you are a beginner, then this chapter will get you started with MFC programming. If you have some programming experience with MFC, you still may want to revisit the concepts. If you are a real expert in MFC programming, then you might want to skip this chapter, and the following four chapters of the overview section, and proceed to the Professional Projects section of this book.

NOTE

Following the overview section that deals with the basics of VC++ programming (MFC) is the section of Professional Projects. In this section, you will learn the new concepts of VC++ .NET by creating various projects based on realistic scenarios.

Overview of MFC Programming

Before delving into the intricacies of programming using MFC, a brief overview of the evolution of MFC is in order. Way back when the IT industry was in its formative years, programmers were having fun programming in DOS. But, as

Team-Fly®

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

21

 

 

 

 

 

time went by, with increases in the complexity of the applications being developed, the complexity of programming in DOS also increased. Microsoft took this challenge and came up with a breakthrough: It introduced Windows to the IT industry. Programmers hardly took any time to tune themselves to Windows programming (often referred to as Win32 API programming or SDK programming) because the application programming interface (API) of Windows was written in C, which was already a widely used language. In addition, the Software Development Kit (SDK) that was supplied along with the API eased programming to an extent. However, the SDK had its own drawbacks, which will be addressed later in the chapter.

At the same time, a paradigm shift was happening in the industry, from procedural programming to object-oriented programming (OOP). As OOP gained footage in the market, Microsoft was quick to catch up. It immediately came up with the object-oriented Microsoft Foundation Classes, or MFC. MFC is a library of classes and several other VC++ tools. Some of the MFC classes encapsulate the APIs of SDK. A few of the core classes are discussed in the following sections.

Basics of MFC Programming

To get to the crux of the chapter, the following are the two main distinguishing features of Windows programming in comparison with traditional DOS programming:

Event-driven programming. Unlike DOS applications, Windows programming is based on events. An event, in simple terms, is a notification of an action. Actions can be triggered by a user, such as a user moving the mouse, by the application, or by the system itself. In a nutshell, a Windows application is completely controlled by events — the application is designed to react to events.

Device independence. This feature is a boon for programmers. If you have programmed in DOS, you remember that you had to tailor the code individually for handling different devices, such as the screen or printer. No more do you need to duplicate your efforts in handling different devices. In Windows programming, the code you write can run on any device, a VDU or a printer.

22

Part I

INTRODUCING VC++.NET

 

 

 

You will be learning how to implement these features using MFC in subsequent sections.

Although Windows programming had its own advantages, the programmers didn’t have a gala time coding in SDK, for several reasons: the length of the code, the number of events to be trapped, and, perhaps foremost, the number of parameters that they had to remember. The only advantage to working with the SDK is that you have complete control over your application — you can design it and make it work the way you want. But all that comes at the cost of time, which is really valuable in such a highly competitive market.

Microsoft, the pioneers in breakthroughs, came up with a solution. As the paradigm shift happened from structured programming to OOP, Microsoft designed MFC, a huge relief to programmers. The main thrust of MFC was to reduce the amount of coding while still enabling the development of highly efficient applications. To accomplish this, MFC provides a framework that handles the default implementation of all Windows applications. In addition, it provides you with numerous classes that can be used to add the required functionality to your application. MFC is a tool that helps you to cut down development time to a great extent.

The following sections give you in-depth details about how each of the previously mentioned features is handled using MFC.

Event Handling

As stated earlier, Windows programming is event-driven. But, how does Windows actually implement this? How can you add the event-handling functionality to your application? Well, in response to each of the events, such as a mouse click or mouse move, Windows generates a “message” to notify the corresponding window about the event. On receipt of a message, the respective window accomplishes the appropriate task specified in the message handler. A message handler is a function that consists of the code for the action to be taken for a specific event. For instance, you might want your application to display a message when the user moves the mouse. Here, “mouse move” is the event and “displaying a message” is the action.

Before delving into the details about messages, you will find out how MFC enables you to handle messages. MFC provides you with a set of classes that encapsulates all required functionalities for message handling and many other

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

23

 

 

 

 

 

functionalities. Figure 2-1 depicts the hierarchy of some of the most important MFC classes that you will be using in your application.

 

 

 

 

 

 

CObject

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CCmdTarget

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CWinThread

 

 

CWnd

 

 

CDocument

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CWinApp

 

 

CFrameWnd

 

 

CView

 

 

CDialog

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CMDIFrameWnd CMDIChildWnd CMiniFrameWnd

FIGURE 2-1 MFC class hierarchy

The following is an overview of some of the main classes in the given hierarchy:

CObject. From the hierarchy shown in Figure 2-1, you can easily infer that the CObject class forms the root of the complete MFC class hierarchy. Two of the most predominant features offered by the CObject class are support for runtime classes and support for serialization (you will learn more about these features in Chapter 5).

CCmdTarget. MFC supports a message map architecture that helps you in coding your application to handle messages. In simple terms, a message map handles the routing of messages to the appropriate message handler. The CCmdTarget class acts as the base class for the message map. Hence, any class that has to handle messages needs to be derived from this class. By default, all key classes of the framework, such as the application class and the window class, are derived from the CCmdTarget class.

CWinApp. Every Windows application has an application class that encapsulates the methods required to handle the initialization tasks of your application and those required to run your application. The CWinApp class provides the default methods for initializing and executing your application, and hence acts as the base class for the application class of

24

Part I

INTRODUCING VC++.NET

 

 

 

your application. You can have only one CWinApp derived class in an application. You will learn about the member functions of the CWinApp class in the following section.

NOTE

The CWinApp class encapsulates the WinMain function of the SDK. This function’s life span starts with the construction of the application object and ends with the destruction of the application object.

CWnd. The CWnd class provides the functionalities required for handling windows and hence acts as the base class for all window classes.

NOTE

The CWnd class, along with the message map architecture, encapsulates the WndProc function of the SDK.

CFrameWnd. What is a Windows application without a window, right? The CFrameWnd class is one of the MFC classes that helps you to create windows for your application. So, to attach a window to your application, you have to derive a class from the CFrameWnd class. You can use either the Create or the LoadFrame member function for creating a frame window. The frame window is created in two steps. First, the constructor of the CFrameWnd derived class is invoked. Second, the Create function, within the constructor, is invoked. The Create function creates the frame window, initializes the window’s class name, registers the values assigned for the style of the window, its parent window, and associated menu, and attaches it to your application.

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

25

 

 

 

 

 

NOTE

Apart from the CFrameWnd class, MFC provides you with two other classes, CMDIFrameWnd and CMDIChildWnd, for creating a frame window for your application. You will learn about these classes in Chapter 5.

Moving ahead, you will learn more about the CWinApp class and then about the message maps and how they enable you to handle messages.

The CWinApp Member Functions

The CWinApp class has three member functions that handle the basic functionality offered by this class:

InitInstance. You need to override the InitInstance function to initialize your application. As soon as you execute your application, the framework’s WinMain function passes the control to the InitInstance function, which then performs the initialization tasks for the current instance of the application. Initialization is done in two parts: one-time initialization and instance initialization. One-time initialization is performed when you first execute your application. On the other hand, instance initialization is performed each time an instance of the application is created (each time you execute the program), including the first time.

CAUTION

You must override the InitInstance function in your application class.

Run. The Run function obtains all messages directed to the application and handles the message routing until it receives the WM_QUIT message. When there is no message queued up in your application’s message queue, the Run function invokes the OnIdle function. In short, the Run function implements the default message loop for your application.

26

Part I

INTRODUCING VC++.NET

 

 

 

CAUTION

Unless you want to provide some special behavior to your application, you shouldn’t override the Run function.

ExitInstance. The ExitInstance function is invoked from within the Run function, once it receives the WM_QUIT message, to quit from the application. You override this function to add any cleanup functionalities for your application.

CAUTION

The ExitInstance function should be invoked only from within the Run function.

The order in which the functions of the CWinApp class are invoked is depicted in Figure 2-2.

Application executed

WinMain function invokes

InitInstance function

WinMain function invokes

Run function

Run function invokes

ExitInstance function

FIGURE 2-2 The flow of control within the member functions of the CWinApp class

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

27

 

 

 

 

 

Now that you have some exposure to (or a review of ) the basic concepts of MFC programming, you are ready to create an application using the classes discussed so far. The following exercise takes you through the steps to create a project in Visual C++ .NET. You will learn to code the application using the MFC classes subsequently.

1.Choose File, New, Project. In the New Project dialog box, shown in Figure 2-3, select the project template Win32 Project. In the Name box, type SampleApplication. The Win32 Application Wizard appears.

FIGURE 2-3 The New Project dialog box

2.In the left section, click on Application settings. In the right section of the Application settings screen, under Additional options, check Empty project, as shown in Figure 2-4.

NOTE

To begin with, you will create an empty project and add the required code. In the subsequent chapters, you will be creating applications using the wizards.

28

Part I

INTRODUCING VC++.NET

 

 

 

FIGURE 2-4 The Application Settings screen of the wizard

3.Click on the Finish button.

4.Choose File, Add New Item to add a .cpp file to the project. In the Add New Item dialog box, shown in Figure 2-5, select C++ File (.cpp). In the name box, enter Sample as the name of the cpp file.

5.In the Solution Explorer, right-click on SampleApplication (the name of the project) and choose Properties. In the Property Pages dialog box (shown in Figure 2-6), under Project Defaults, set the value for Use of

MFC as Use MFC in a Shared DLL.

Here is a code snippet of a sample application that illustrates the implementation of the CFrameWnd and CWinApp classes:

# include <afxwin.h>

class MyWindow:public CFrameWnd

{

public:

MyWindow()

{

Create(NULL,”My Application Window”);

MessageBox(“Window Created”, “CFrameWnd

BASICS OF VC++.NET PROGRAMMING

Chapter 2

 

29

 

 

 

 

 

FIGURE 2-5 The Add New Item dialog box

FIGURE 2-6 The Property Pages dialog box of the project