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

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

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать

350 Project 2 CREATING AN APPLICATION USING MANAGED C++

Menu bar File Edit Pen View Draw Window Help

Toolbar

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

New

 

Open Save Help

Y

 

 

 

 

 

 

 

 

 

Print preview

 

 

 

 

 

 

 

 

 

 

L

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

F

 

 

Drawing

 

 

 

 

 

 

 

M

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

T

 

 

 

 

 

 

FIGURE 12-1 A sketch forEthe screen of the Easy Tool application

 

 

File

Edit

Pen

View

Draw

Window

Help

New

 

 

 

 

 

 

Open

 

 

 

 

 

 

Close

 

 

 

 

 

 

Save

 

 

 

 

 

 

SaveAs

 

 

 

 

 

 

Print

 

 

 

 

 

 

Print

 

 

 

 

 

 

preview

 

 

 

 

 

 

Exit

 

 

 

 

 

 

FIGURE 12-2 The drop-down list for the File menu button

Team-Fly®

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 351

The drop-down list for the Edit menu button is displayed in Figure 12-3.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

File

 

Edit

 

Pen

 

View

 

Draw

 

Window

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Clear all

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FIGURE 12-3 The drop-down list for the Edit menu button

The drop-down list for the Pen menu button is displayed in Figure 12-4. The drop-down list for the View menu button is displayed in Figure 12-5.

352 Project 2 CREATING AN APPLICATION USING MANAGED C++

File

 

Edit

 

Pen

 

View

 

Draw

 

Window

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

Thick line

Pen width

FIGURE 12-4 The drop-down list for the Pen menu button

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

File

 

Edit

 

Pen

 

View

 

Draw

 

Window

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Toolbar

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Status

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

bar

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FIGURE 12-5 The drop-down list for the View menu button

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 353

The Figure 12-6 displays the drop-down list for the Draw menu button.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

File

 

Edit

 

Pen

 

View

 

 

Draw

 

 

Window

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Line

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Ellipse

Rectangle

Text

Filled ellipse

Filled rect

FIGURE 12-6 The drop-down list for the Draw menu button

The drop-down list for the Window menu button is displayed in Figure 12-7.

The drop-down list for the last menu button, Help, is displayed in Figure 12-8.

The Coding and Construction Phase

In this phase, the proposed design of the application is translated into code. If the application is big, the team could be divided into smaller, separate teams, each of which could be given individual tasks. This is the longest phase of the DLC.

The Testing Phase

The application is tested in this phase, which is the fourth phase of the DLC. The modules in the application may be tested individually or at one go. In the case of the Easy Tool application, the team decided to test the complete tool in one go. The development team decided on a test plan outline that consists of details such

354 Project 2 CREATING AN APPLICATION USING MANAGED C++

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

File

 

Edit

 

Pen

 

View

 

Draw

 

Window

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

New

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

window

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cascade

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Title

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FIGURE 12-7 The drop-down list for the Window menu button

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

File

 

Edit

 

Pen

 

View

 

Draw

 

Window

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Help

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

topics

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

About

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

GDIPlus

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FIGURE 12-8 The drop-down list for the Help menu button

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 355

as the requirements for the project, items to be tested, test deliverables, environmental needs, and so forth. A test specification document is also prepared to help the testers check the application. The test specification document lists the specifications for testing the application.

The Execution Phase

This is the final phase in the DLC of the application. After the application has been successfully tested, it needs to be distributed so that all departments in the institute have access to the project. The development team of the application has the responsibility of providing constant support in terms of installation and debugging errors, if any, to all faculty members using the application.

Coding the Application

As discussed in the previous section, the Easy Tool application comprises a number of screen elements. Before delving deep into the code behind the application, let me first explain how to create a Managed C++ application and the interface elements of the application (the menus and the toolbar). After discussing the creation of these elements, I will discuss how to programmatically add functionality to these elements.

Creating the Application

To create a Managed C++ application, follow these steps:

1.Choose File, New Project.

2.From the list of projects displayed, select Managed C++ Application and enter the name of the application. By default, the environment creates the <app name>.cpp file, the AssemblyInfo.cpp file, and the stdafx.cpp and .h files.

3.Add the required files to the project based on your requirements.

Creating the User Interface Form

In a number of instances in an application, user input has to be accepted. You can accept user input using forms and the Windows controls that are placed on these

356 Project 2 CREATING AN APPLICATION USING MANAGED C++

forms. Some of the important elements that are required to accept user input are listed here:

Form. The container that holds all the other controls; represented by the Form class.

Button. Used to represent a Windows form button. The functionality for the button is encapsulated in the Button class. It is the base class for

the RadioButton and CheckBox classes.

TextBox. The representation of the Windows text box control and is used to accept user input; represented by the TextBox class.

Label. Used to display static text; represented by the Label class.

In the following sections, I will discuss the important properties and methods of all of these classes.

Form Class

The Form class encapsulates the functionality associated with creating and displaying a form. Some of the important properties of the Form class are listed in Table 12-1.

Table 12-1 Properties of the Form Class

Property

Description

ActiveForm

Returns the currently active form for the application.

AutoScale

A Boolean value. Is set to true to indicate that the form will scale

 

itself automatically based on the size of the current font and

 

dimensions.

AutoScaleBaseSize

Used to set or retrieve the base size to be used to scale the form.

Controls

Retrieves the collection of controls associated with the form.

Icon

Used to set or retrieve the icon for the form.

Menu

Sets or retrieves the main menu associated with the form.

Size

Sets or retrieves the size of the form.

Text

Sets or retrieves the text associated with the form.

MaximizeBox

Indicates whether the Maximize box is displayed on the form.

MinimizeBox

Indicates whether the Minimize box is displayed on the form.

 

 

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 357

Button Class

As previously discussed, the Button class encapsulates the functionality associated with creating and displaying a button. Table 12-2 lists the important methods of the Button class.

Table 12-2

Properties of the Button Class

Property

Description

 

 

DialogResult

Sets or retrieves the value that is returned to the parent control

 

when the button is clicked.

Location

Sets or retrieves the coordinates of the upper-left corner of the

 

control relative to the position of the container.

Name

Sets or retrieves the name of the control.

Size

Sets or retrieves the height and width of the control.

TabIndex

Sets or retrieves the tab order of the control within the container.

Text

Sets or retrieves the text associated with the control.

 

 

TextBox Class

The TextBox class is used to accept user input. Some of the important properties of the TextBox class are listed in Table 12-3.

Table 12-3 Properties of the TextBox Class

Property

Description

Location

Sets or retrieves the coordinates of the upper-left corner of the

 

control relative to the position of the container.

Name

Sets or retrieves the name of the control.

Size

Sets or retrieves the height and width of the control.

TabIndex

Sets or retrieves the tab order of the control within the container.

Text

Sets or retrieves the text associated with the control.

 

 

358 Project 2 CREATING AN APPLICATION USING MANAGED C++

Label Class

The Label class is used to display static text. Some of the important properties of the Label class are listed in Table 12-4.

Table 12-4

Properties of the Label Class

Property

Description

 

 

Location

Sets or retrieves the coordinates of the upper-left corner of the

 

control relative to the position of the container.

Size

Sets or retrieves the height and width of the control.

TabIndex

Sets or retrieves the tab order of the control within the container.

Text

Sets or retrieves the text associated with the control.

 

 

Having looked at the important properties of these classes, you now are ready to take a look at the following code snippet from the Easy Tool application that creates the user interface, a form with textbox, label, and button controls:

Form* f = new Form();

//Get the document of active view

CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*> (this->ActiveMdiChild); CGDIPlusDoc* pActiveDoc = pActiveView->GetDocument();

f->AutoScaleBaseSize = System::Drawing::Size(5, 13);

f->Text = “Pen Width”;

f->ClientSize = System::Drawing::Size(352, 125);

Button* button1 = new Button();

button1->Location = System::Drawing::Point(264, 20); button1->Size = System::Drawing::Size(75, 23); button1->TabIndex = 1;

button1->Text = “OK”;

button1->DialogResult = System::Windows::Forms::DialogResult::OK ;//Make this “OK” button

Button* button2 = new Button();

button2->Location = System::Drawing::Point(264, 52);

CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 359

button2->Size = System::Drawing::Size(75, 23); button2->TabIndex = 6;

button2->Text = “Cancel”;

TextBox* textBox1 = new TextBox();

textBox1->Location = System::Drawing::Point(120, 36); textBox1->Text = pActiveDoc->nThinWidth.ToString(); textBox1->TabIndex = 1;

textBox1->Size = System::Drawing::Size(64, 20);

TextBox* textBox2 = new TextBox();

textBox2->Location = System::Drawing::Point(120, 76); textBox2->Text = pActiveDoc->nThickWidth.ToString(); textBox2->TabIndex = 2;

textBox2->Size = System::Drawing::Size(64, 20);

Label* label1 = new Label();

label1->Location = System::Drawing::Point(16, 36); label1->Text = “Thin Pen Width:”;

label1->Size = System::Drawing::Size(88, 16); label1->TabIndex = 3;

Label* label2 = new Label();

label2->Location = System::Drawing::Point(16, 76); label2->Text = “Thick Pen Width:”;

label2->Size = System::Drawing::Size(95, 16); label2->TabIndex = 4;

f->FormBorderStyle = FormBorderStyle::FixedDialog;

//Set the MaximizeBox to false to remove the maximize box. f->MaximizeBox = false;

//Set the MinimizeBox to false to remove the minimize box. f->MinimizeBox = false;

//Set the accept button of the form to button1.

f->AcceptButton = button1;

// Set the cancel button of the form to button2. f->CancelButton = button2;