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

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

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

n Chapter 2, you learned the basic concepts of VC++.NET programming. In

Ithis chapter, you will learn to create a user interface. You will be introduced to

the MFC Application Wizard, which is used to create a framework of the user

interface for an application. The chapter covers the different classes that you need

 

 

L

for an MFC application framework and also discusses the procedure to customize

 

 

F

an application that the wizard generated. ou will also learn how to handle events

for controls.

 

M

Y

 

 

 

A

 

 

E

 

An Introduction to the MFC Application

Wizard

T

 

 

 

 

 

The simplest way to create an MFC application is to use the MFC Application Wizard. By using the wizard, you create a framework for an application. Based on your input in the different pages of the wizard, the application framework includes the objects of the required classes for an application. After the framework for the application is ready, you can customize it by adding the required objects and code for handling different activities in the application.

Using the MFC Application Wizard consists of two steps:

1.Start the MFC Application Wizard.

2.Create an application framework by using the MFC Application Wizard.

Each of these processes is discussed next in detail.

Starting the MFC Application Wizard

When you start Visual Studio.NET for the first time, a Start page is displayed. On this page, you can configure the default project type that you want to open when you start Visual Studio.NET from next time onward. For example, if you select Visual C++ Projects, Visual Studio.NET opens Visual C++ Projects by default.

Team-Fly®

CREATING A USER INTERFACE

Chapter 3

51

 

 

 

To create an application using the MFC Application Wizard, you need to perform the following steps:

1.Select File, New, Project to open the New Project dialog box.

2.Select Visual C++ Projects from the Project Types list. If you have already configured Visual C++ Projects as a default selection of Visual Studio.NET, you can skip this step. The Template list displays the wizards for creating different types of applications in Visual C++.

3.Select the MFC Application icon from the Templates list.

4.Type MyFirstApplication in the Name text box. An application named MyFirstApplication is created in the default folder of Visual Studio.NET. To change the default folder, you can either type the path of the required folder in the Location text box or click on Browse to locate and specify the required folder.

5.Click on OK to start the wizard.

The MFC Application Wizard starts with options that are almost similar to VC++ 6. However, a significant difference between the MFC Application Wizard of VC++.NET and the MFC AppWizard of VC++ 6 is that the wizard guides you through a series of six steps for applying properties for an application in VC++ 6, whereas in VC++.NET, all the properties for an application are available in the same dialog box.

The MFC Application Wizard has two frames, as shown in Figure 3-1. The left frame consists of the links for the different types of properties, such as Application Type, Database Support, User Interface Features, and Generated Classes. To set specific properties for an application, you need to click on the related property type. The right frame displays the related properties of the property type you select. By default, the Overview link is selected. It displays the overview of the properties that you select for an application.

Creating an Application Framework

To create an application by using the MFC Application Wizard, you need to provide some basic information for the properties that you want to set for your application. For example, if you need to create a Multiple Document Interface (MDI) application, you need to select the Multiple Documents option in the Application Type page.

52

Part I

INTRODUCING VC++.NET

 

 

 

FIGURE 3-1 The MFC Application Wizard displaying the Overview page

To create a Single Document Interface (SDI) application without database support, you need to do the following:

1.Click on the Application Type link in the left frame of the MFC Application Wizard. The right frame displays the properties that you can specify for an application. In the Application Type page, you select an application type, project style, use of MFC, and Document/View architecture for an application, as shown in Figure 3-2.

2.Select Single document under Application type. The following list describes when you would select each of the options under Application type:

Single document. Select to create an application that enables you to open only one document at a time, such as the Notepad application of Windows.

Multiple documents. Select to create an application that enables you to open multiple documents at a time, such as Microsoft Word, Excel, and PowerPoint applications.

Dialog based. Select to create an application that enables you to create a form-like application.

CREATING A USER INTERFACE

Chapter 3

53

 

 

 

FIGURE 3-2 The MFC Application Wizard displaying the Application Type page

Multiple top-level documents. Select to create an application that enables you to create an application that opens a new window when you select File, New from the application menu.

In the Application Type page, you also specify the type of project you want to create, such as a Windows Explorer-based or standard MFC application. In addition to the application type and project style, you can also specify the use of the MFC library. You can link the MFC library to your application as a shared DLL, or to the static MFC library at build time.

3.Click on the Compound Document Support link in the left frame of the MFC Application Wizard. The right frame displays the properties for compound document support. Because your application need not have compound document support, you need not change the default option.

4.Click on the Document Template Strings link in the left frame of the MFC Application Wizard. The right frame displays the properties for the management of the documents that your application creates. For example, you can specify an extension for the documents that you create with your application.

54Part I INTRODUCING VC++.NET

5.Click on the Database Support link in the left frame of the MFC Application Wizard. The right frame displays the properties for the database support, client type, and data source, as shown in Figure 3-3. Since your application does not require any database support, you need not change the default option.

FIGURE 3-3 The MFC Application Wizard displaying the Database Support page

TIP

If you select the Dialog based option under Application type, the Compound Document Support, Document Template String, and Database Support links will not be available.

6.Click on the User Interface Features link in the left frame of the MFC Application Wizard. The right frame displays the properties for the user interface of an application.

7.Select the Maximized option in the right frame of the User Interface Features page. When you start an application, this option enables you to open an application in a maximized window.

CREATING A USER INTERFACE

Chapter 3

55

 

 

 

8.Click on the Advanced Features link in the left frame of the MFC Application Wizard. The right frame displays the properties for the advanced features, such as Context-sensitive Help, Print, and other network features for an application. Since your application does not require any network features, you need not change the default options.

9.Click on the Generated Classes link in the left frame of the MFC Application Wizard. The right frame displays the classes that the MFC Application Wizard creates. The Generated Classes page displays the classes, their names, .h and .cpp files, and the corresponding base class, as shown in Figure 3-4.

FIGURE 3-4 The MFC Application Wizard displaying the Generated Classes page

The Generated Classes page of the MFC Application Wizard enables you to create an interface for your application. The interface for your application depends on the base class that you select for the application view. For example, you need to select the following:

The CEditView base class for the application view to create a text editor application, such as Notepad or Microsoft Word

The CFormView base class to create a form-based application

The CHtmlView base class to create a Web-based application

56

Part I

INTRODUCING VC++.NET

 

 

 

TIP

In the Generated Class page, you can edit the name of the class for all classes generated. You can also edit the name of the .h file and the .cpp file of all classes other than the application class. In addition, you can change the base class for only the view class of the application framework.

10.Select CFormView from the Base class drop-down list. You will create a form-based application.

11.Click on Finish to close the MFC Application Wizard. A message box stating that no printing support will be available to CFormView is displayed. Click on Yes to close the message box.

When you create an application framework, the MFC Application Wizard creates the necessary classes for the framework. The wizard creates the following classes for your application:

CMainFrame

CMyFirstApplicationApp

CMyFirstApplicationDoc

CMyFirstApplicationView

CAboutDlg

These classes are discussed in detail next.

CMainFrame

The CMainFrame class is one of the most important classes that the MFC Application Wizard generates for your application. This class is derived from the CFrameWnd class that defines all the necessary handlers and functions to handle the messages that are targeted to an application window. For any window-based application, you have to create a class that is derived from the CFrameWnd class. In your application, this class handles the layout for the child windows, including toolbars and the status bar, and the creation and destruction of the application window.

CREATING A USER INTERFACE

Chapter 3

57

 

 

 

CMyFirstApplicationApp

In addition to the CMainFrame class, the wizard also generates CMyFirstApplicationApp, which is derived from the CWinApp class. The CWinApp derived class displays the window that you create by using the CFrameWnd derived class. The CWinApp class consists of the InitInstance function, which is the first function to be executed when you run an application. You need to override this function in the derived class to create an object of the CFrameWnd derived class.

CMyFirstApplicationDoc

CMyFirstApplicationDoc is yet another important class that the MFC Application Wizard generates. The CMyFirstApplicationDoc is derived from the CDocument class. You need to create a CDocument derived class for your application to handle the application data. The CDocument class consists of the OnSaveDocument function that the application executes when you select File, Save or File, Save As. The OnSaveDocument function saves the document data of your application. The tasks that the CDocument derived class performs include creating, retrieving, saving, and closing documents. The CDocument derived class is also responsible for determining the modification of the document data.

The CDocument derived class implements the concept of Document/View architecture in an application. In the Document/View architecture, the CDocument derived class accesses data from a document, and the CView derived class presents the data on the user interface.

CMyFirstApplicationView

To display the data of the application document, you need to create a view. To do so, you need to create a class that is derived from CView or any of the CView derived class, such as CEditView, CFormView, or CHtmlView class. The CView class consists of OnDraw and OnChar functions. The OnDraw function helps an application to draw the images, and the OnChar function helps an application to display characters on the client area of an application. The OnDraw and OnChar functions together display the data stored in the application document.

You can further create views of different types depending on the requirements of an application. For example, to create a text editor application, such as Notepad or Microsoft Word, you need to create a CEditView derived class. To create a formbased application, you need to create a CFormView derived class. A form-based application consists of different controls, such as text boxes, buttons, check boxes,

58

Part I

INTRODUCING VC++.NET

 

 

 

and static text. To create a Web-based application, you need to create a CHtmlView derived class.

CAboutDlg

The CAboutDlg is the CDialog derived class. You use this class to display the About MyFirstApplication help. The dialog box is displayed when a user selects Help, About MyFirstApplication. In addition to the default contents of the dialog box, you can customize the contents of the dialog box.

Executing the Application

To build and execute the application that you have created, you need to perform the following steps:

1.Select Build, Build to compile the application. The output frame displays that the compilation is successful and the application does not contain any errors.

2.Select Debut, Start to execute the application. The maximized MyFirstApplication – Untitled window is displayed, as shown in Figure 3-5. The view of the application is derived from the CFormView class and indicates that you need to add controls to the view.

FIGURE 3-5 The output of the application created by the MFC Application Wizard

CREATING A USER INTERFACE

Chapter 3

59

 

 

 

To this point, you have learned to create an application framework by using the MFC Application Wizard. Next, you will discover how to customize the framework to create your application.

Customizing an Application Framework

When you create an application by using the MFC Application Wizard, the wizard just creates a framework for the application. It has all the basic functionality of a window application. To customize this framework, you need to add the required functionality to the application.

To customize an application, one of the important steps is to customize the view class of your application. Since you have derived the view class from the CFormView class, you need to add the required controls to design the view.

Before adding controls, consider a scenario. You need to create an application that can store employee information, such as the employee code, first name, last name, address, date of birth, and date of joining the organization. When a user clicks on the Submit button, the data is transferred to the member variables, and the Reset button resets the default values of the controls.

In your application, the MFC Application Wizard has generated the IDD_MYFIRSTAPPLICATION_FORM form. To add controls to this form, you need to do the following:

1.Select View, Resource View to display the resources for the application. Resource View consists of all the resources, such as the menus icons and dialog boxes of an application.

2.Expand the MyFirstApplication application in Resource View.

3.Expand the MyFirstApplication.rc resources in Resource View.

4.Expand Dialog in Resource View. The Dialog section consists of two dialog resources, IDD_ABOUTBOX and IDD_MYFIRSTAPPLICATION_FORM.

5.Double-click on IDD_MYFIRSTAPPLICATION_FORM. The form opens in the Dialog Editor, as shown in Figure 3-6. The Dialog Editor enables you to add controls to the form.

6.Select View, Toolbox to display the Toolbox. The Toolbox consists of the different controls, such as the Edit control and Button control, which you can add to your form.