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

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

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

80

 

Part I

INTRODUCING VC++.NET

 

 

 

 

 

Working with Dialog Boxes and

Controls

Dialog boxes are mostly used as extensions of menus. This implies that dialog boxes are used to accept any additional information besides choosing the appropriate menu option. For instance, while working in Microsoft Word, if you choose

Y L options, such as options to select theFtype of line to be drawn (for example, dot-

File, Save, the Save As dialog box is displayed, which accepts information such as

the file name. Similarly, considering the application for which you designed the

Draw menu option, you might customize it further by providing additional

ted or straight line, the color to be used, and the length and thickness of the line). To provide such additional features, you use dialog boxes. In most cases, dialog boxes consist of the user interface that is similar to a form. You can use various

controls in a dialog box for accepting and displaying the user input. Table 4-1 dis-

plays the different controls thatMyou can use in a dialog box.

 

A

Table 4-1 Common DialogEBox Controls

Control

Used to

 

T

Static text

Display static text such as a label for a form control. It does not accept any

 

information from the user.

Edit box

Accept user input in the form of text. For example, you use an edit box to accept

 

the login ID or name of the user. The edit box is also known as a text box.

List box

Display a list of items to the user. You can set the properties of the list box to

 

enable the user to select multiple items from the list.

Combo box

Display a list of items to the user. It is a combination of an edit box and a list

 

box. Therefore, the user can either select from the list or type a new item. The

 

combo box is also known as a drop-down list.

Radio button

Select an item from a group of items. You can select only one radio button

 

from a group. A radio button depicts a true or a false status.

Check box

Select multiple items from a group of items. Similar to a radio button, a check

 

box also depicts a true or a false status.

Group box

Group a set of controls in a dialog box.

Push button

Perform an action. For example, in most dialog boxes, you use OK to accept

 

the user input and Cancel to discard the user input.

 

 

Team-Fly®

EXAMPLE ILLUSTRATING THE USE OF A GROUP BOX
Consider a situation in which you place radio buttons for Male, Female, Red, Blue, and Green. The dialog box will enable you to select any one of the five options. However, the dialog box should enable you to select an option for gender and another for a color. To do so, you can place the related radio buttons in separate group boxes.

MENUS, DIALOG BOXES, AND WINDOW CONTROLS

Chapter 4

 

81

 

 

 

 

 

Figure 4-3 displays the Print dialog box, which is a classic example of a Microsoft Windows dialog box containing several of the different controls listed in Table 4-1.

In any application, you can use three types of dialog boxes:

Application modal dialog box. Has the control of the application in which it is displayed.

This implies that you cannot continue working with the current application until you dismiss the dialog box. Once again taking the example of the Save As dialog box, do you ever remember typing text while this dialog box is open? The same applies to many other dialog boxes, such as the Print and Open dialog boxes. All of these dialog boxes are examples of the application modal dialog box. Although you can’t interact with the current application in which the modal dialog box is open, you can continue working with the other active applications.

FIGURE 4-3 The Print dialog box with different controls

82Part I INTRODUCING VC++.NET

System modal dialog box. Unlike application modal dialog box, controls the entire system instead of only the application. This implies that you can neither work with the current application nor switch to any other active applications until you dismiss the dialog box. For example, while working with Visual Studio .NET, you might have to access the Visual Studio .NET documentation. To do so, you open the Start menu. However, accidentally, instead of choosing Programs, you choose Shut Down. As a result, the Shutdown dialog box appears. Once this dialog box appears, the entire focus of your system shifts to the dialog box. Hence, you will not be able to access any application until you dismiss it. Thus, the Shutdown dialog box can be categorized as a system modal dialog box.

Modeless dialog box. The most flexible type of dialog box you can create, the simple reason being that it allows the user to interact with both the current and other active applications even when the dialog box is active. A classic example of the modeless dialog box is the Find and Replace dialog box.

Moving forward, you will create an application that displays a login dialog box. To differentiate between modal and modeless dialog boxes, you will first design the login dialog box as a modal dialog box. Later, you will design the same dialog box as a modeless dialog box, to understand the difference in their functionality.

Creating a Modal Dialog Box

As discussed earlier, when a modal dialog box is active, the control of the application lies with the dialog box. Therefore, any message generated for the application is directed to the dialog box. For example, when the Save As dialog box is active, if you click anywhere else within the application window, you hear a sound but there is no response from the application. This occurs because the message that was intended for the application window is now redirected to the dialog box, but the dialog box is not capable of handling the message.

To create a modal dialog box, you need to perform the following tasks:

1.Create a dialog box resource.

2.Create a dialog box class to handle the dialog box messages.

These tasks are discussed next in detail. You will be designing an MDI application with a menu and a dialog box. This application will be based on the

MENUS, DIALOG BOXES, AND WINDOW CONTROLS

Chapter 4

 

83

 

 

 

 

 

CEditView class and hence will have the basic functionality of a text editor. You’ll learn more about MDI applications and the view classes in Chapter 5.

Creating a Dialog Box Resource

To create a dialog box resource for this example, you need to perform the following steps:

1.Create an MDI application using the MFC Application Wizard. The sample application shown here is named DialogApp. In the left frame of the wizard, click Generated classes and change the base class of

CDialogAppView to CEditView.

2.Switch to the Resource View of the application and, from the list of resources, select Dialog. By default, the wizard provides a dialog box,

IDD_ABOUTBOX. As the name indicates, it displays information about the application itself.

3.Select Project, Add Resource to open the Add Resource dialog box. From the list of resources, select Dialog. The Dialog Resource type provides some predefined layouts for different dialog resources, such as layouts for form view, dialog bar, and property pages. You can either use these predefined resources or create a new one for an application.

4.Click on the New button to create a new dialog resource. A new dialog resource with an ID of IDD_DIALOG1 is added to your application. The dialog box consists of the default OK and Cancel buttons.

5.Add controls to the dialog resource as shown in Figure 4-4. Change captions for the static text controls as shown in the figure, and change the ID of the edit boxes to IDC_LOG and IDC_PASS, respectively. For the password field, change the Password property to True.

In most of the cases, you open a dialog box by using a menu command. So, you will now edit the default menu to add another option that will invoke the dialog box you have created:

1.Click the Menu resource in the Resource Window.

2.Select the menu resource IDR_DialogAppTYPE. Add the option, Logon, as shown in Figure 4-5.

84

 

Part I

INTRODUCING VC++.NET

 

 

 

 

 

FIGURE 4-4 The dialog resource

FIGURE 4-5 The menu resource

After you have created the resources for the application, the framework for the application is ready to be used. To display the dialog box that you have created, you need to code for the dialog box class. The steps to add a dialog class are detailed next.

Creating a Dialog Box Class

The CDialog class, which is derived from the CWnd class, provides you the functionality for displaying a dialog box and handling messages that are directed to the dialog box. Therefore, for creating any type of dialog box in MFC, you need to

DoDataEx-

MENUS, DIALOG BOXES, AND WINDOW CONTROLS

Chapter 4

 

85

 

 

 

 

 

derive a class from the CDialog class. This class provides you with two member functions, DoModal and Create, that you use to display a modal and a modeless dialog box, respectively.

In addition to displaying the dialog box, you also need to enable the dialog box class to handle user interaction with it. This implies that you will have to code to store and retrieve data that the user enters in the dialog box controls. Remember how you did this while using forms in Chapter 3? You had declared member variables corresponding to each of the controls. Similarly, for the controls of the dialog box, you can create member variables that will hold the data. The Add Variable Wizard enables you to add a variable for a control, and the

change function of the CWnd class enables you to exchange data between the controls and the corresponding member variables.

Here’s what you need to do to add a dialog class to your application:

1.Choose Project, Add Class. The MFC Class Wizard, as shown in Figure 4-6, starts.

2.Type “DialogClass” in the Class name box of the wizard and select CDialog from the base class list.

3.Click on the Finish button to close the wizard. The wizard adds two files to your project, DialogClass.h and DialogClass.cpp, which contain the class declaration and definition, respectively.

FIGURE 4-6 The MFC Class Wizard

86

 

Part I

INTRODUCING VC++.NET

 

 

 

 

 

The MFC Class Wizard creates an enumerator IDD for the dialog resource ID in the header file:

enum { IDD = IDD_DIALOG1 };

An enumerator is a type-declared constant for an integer value. Here, the enum keyword declares a constant IDD for the dialog resource IDD_DIALOG1. You can use IDD when you need to refer to the IDD_DIALOG1 resource.

Besides this, the wizard also links the user-defined dialog resource to the DialogClass by invoking the base class constructor and passing IDD as the parameter, which represents the user-defined dialog box.

DialogClass::DialogClass(CWnd* pParent /*=NULL*/)

: CDialog(DialogClass::IDD, pParent)

,log(_T(“”))

,pass(_T(“”))

{

}

Next, you need to add member variables corresponding to each of the controls in the dialog resource. To do so:

1.Switch to the dialog resource.

2.Right-click on the control for LoginId and select Add Variable from the shortcut menu. The Add Variable Wizard starts.

3.Select Value from the Category list. Correspondingly, the value in the Variable type list is set by default to CString.

4.Type “log” in the Variable name box and click on the Finish button.

5.Similarly, add a variable named “pass” for the Password edit control.

The Add Variable Wizard automatically declares these variables in the header file containing the class declaration. The wizard also automatically generates the code for implementing the DDX mechanism that is used to transfer data from controls to variables and vice versa. This code is added to the DoDataExchange function. The following is the code snippet of the DoDataExchange function:

virtual void DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//Code to transfer data from controls to

MENUS, DIALOG BOXES, AND WINDOW CONTROLS

Chapter 4

 

87

 

 

 

 

 

member variables

DDX_Text(pDX, IDC_LOG,log);

DDX_Text(pDX, IDC_PASS,pass);

}

The DoDataExchange function is a member of the CDialog class. This function accepts a CDataExchange type of object as a parameter. When you create a new dialog class using the AddClassWizard, it automatically overrides the DoDataExchange function of the base class, CDialog.

Finally, before you can use the dialog box functionality, you need to add the appropriate message handlers. All you need to do is the following:

1.In the dialog resource, right-click on the OK button.

2.Select Add Event Handler from the shortcut menu to start the Event Handler Wizard.

3.Click on the Add and Edit button to edit the message handler.

4.Edit the message handler as follows:

void OnBnClickedOk

{

//Call the DDX mechanism UpdateData(TRUE);

MessageBox(“Login name “ + log+” is accepted.”);

//Close the dialog box EndDialog(TRUE);

}

5.Add another message handler for the Cancel button and edit the message handler as follows:

void OnBnClickedCancel()

{

MessageBox(“Closing the dialog box.”);

EndDialog(TRUE);

}

When the user clicks on the OK button, the control is passed to the OnBnClickedOk message handler. In the message handler, the UpdateData function of the CDialog class is invoked. The UpdateData function implements the DDX mechanism by invoking the DoDataExchange function of the CFirstDialog class.

88

 

Part I

INTRODUCING VC++.NET

 

 

 

 

 

Based on the Boolean parameter that you pass to the UpdateData function, the direction of data transfer is decided. If the parameter is TRUE, data is transferred from controls to member variables, and if it is FALSE, then data is transferred from variables to controls. Figure 4-7 explains the implementation of the DDX mechanism.

User clicks the OK button

Calls UpdateData(TRUE)

Sets up

CDataExchange Object

Is passed to

DoDataExchange Function

Of a CDialog

derived class

 

 

 

 

 

 

 

 

DoDataExchange Function Of a CDialog class

Data Exchange

In the CDialog

derived class

 

 

 

FIGURE 4-7 Implementation of the DDX mechanism in a dialog-based application

When the user clicks the Cancel button, the OnBnClickedCancel message handler is invoked, which displays a message box and closes the dialog box.

Following is the code snippet of the dialog class used in the sample application:

// DialogClass.h file

class DialogClass : public CDialog

{

DECLARE_DYNAMIC(DialogClass)

MENUS, DIALOG BOXES, AND WINDOW CONTROLS

Chapter 4

 

89

 

 

 

 

 

public:

// standard constructor DialogClass(CWnd* pParent = NULL); virtual ~DialogClass();

// enumerator for dialog resource ID

enum { IDD = IDD_DIALOG1 };

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

DECLARE_MESSAGE_MAP()

public:

CString log;

CString pass;

afx_msg void OnBnClickedOk(); afx_msg void OnBnClickedCancel();

};

// DialogClass.cpp file #include “stdafx.h” #include “DialogApp.h” #include “DialogClass.h”

IMPLEMENT_DYNAMIC(DialogClass, CDialog)

DialogClass::DialogClass(CWnd* pParent /*=NULL*/)

: CDialog(DialogClass::IDD, pParent)

,log(_T(“”))

,pass(_T(“”))

{

}

// Destructor DialogClass::~DialogClass()

{

}

void DialogClass::DoDataExchange(CDataExchange* pDX)