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

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

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

330

Project 2

CREATING AN APPLICATION USING MANAGED C++

 

 

 

 

 

 

 

 

 

 

 

Table 11-3 (continued)

 

 

 

 

 

 

 

 

 

 

 

Property Name

 

 

Description

 

 

 

 

 

 

 

 

 

TextContrast

 

 

Used to get or set the correction value for the Graphics

 

 

 

 

 

 

 

object.

 

 

 

TextRenderingHint

 

Used to get or set the rendering mode for text for the

 

 

 

 

 

 

 

Graphics object.

 

 

Transform

 

 

Used to get or set the world transformation for the Graphics

 

 

 

 

 

 

 

object.

L

 

 

 

 

 

 

 

 

 

 

VisibleClipBounds

 

Used to get or set the bounding rectangle of the visible clip-

 

 

 

 

 

 

 

ping region of theYGraphics object.

 

 

Table 11-4 lists the methods of the FGraphics class.

 

 

 

 

 

 

E

 

 

 

 

 

 

T

 

 

 

 

Table 11-4 Methods of the GraphicsMClass

 

 

Method Name

 

ADescription

 

 

AddMetafileComment

 

Used to add a comment to the Metafile object.

 

 

BeginContainer

 

 

Used to save a graphic container with the current state of the

 

 

 

 

 

 

 

Graphics object and open a new graphics container. This

 

 

 

 

 

 

 

method can be overloaded.

 

 

Clear

 

 

 

Used to clear the drawing surface area and fill the area with

 

 

 

 

 

 

 

the specified background color.

 

 

CreateObjRef

 

 

Used to create an object to generate a proxy used to communi-

 

 

 

 

 

 

 

cate with a remote object. This method is inherited from the

 

 

 

 

 

 

 

MarshalByRefObject.

 

 

Dispose

 

 

Used to release all the resources taken up by the Graphics

 

 

 

 

 

 

 

object.

 

 

 

DrawArc

 

 

Used to draw an arc that represents a portion of an ellipse.

 

 

 

 

 

 

 

This function can be overloaded.

 

 

DrawCurve

 

 

Used to draw a cardinal spline. This method can be overloaded.

 

 

DrawClosedCurve

 

 

Used to draw a closed cardinal spline. This method can be

 

 

 

 

 

 

 

overloaded.

 

 

 

DrawEllipse

 

 

Used to draw an ellipse that is defined by a bounding rectan-

 

 

 

 

 

 

 

gle. The ellipse is specified by a pair of coordinates. This

 

 

 

 

 

 

 

method can be overloaded.

Team-Fly®

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

331

 

 

 

 

 

 

Method Name

Description

DrawIcon

Used to draw the image that is specified by the Icon object.

DrawIconUnstretched

Used to draw the image specified by the Icon object without

 

scaling the image.

DrawImage

Used to draw the Image object at the specified location. This

 

method can be overloaded.

DrawImageUnscaled

Used to draw the specified Image object with its original size

 

at the specified location. This method can be overloaded.

DrawLine

Used to draw a line. This method can be overloaded.

DrawLines

Used to draw a series of line segments. This method can be

 

overloaded.

DrawPath

Used to draw a GraphicsPath object.

DrawPie

Used to draw a pie shape. This method can be overloaded.

DrawPolygon

Used to draw a polygon. This method can be overloaded.

DrawRectangle

Used to draw a rectangle. This method can be overloaded.

DrawRectangles

Used to draw a series of rectangles. This method can be over-

 

loaded.

DrawString

Used to draw the specified string by using the Brush and

 

Font objects at a specified location. This method can be over-

 

loaded.

EndContainer

Used to close the graphics container and save the state of the

 

Graphics object.

EnumerateMetafile

Used to transfer the records from the Metafile object to a

 

callback method.

FillClosedCurve

Used to fill a closed cardinal spline curve with a specified

 

color.

FillEllipse

Used to fill an ellipse that is defined by a bounding rectangle.

 

This method can be overloaded.

FillPie

Used to fill a pie that is defined by an ellipse with a specified

 

color. This method can be overloaded.

continues

332

Project 2

CREATING AN APPLICATION USING MANAGED C++

 

 

 

 

 

 

 

 

Table 11-4 (continued)

 

 

 

 

 

 

 

 

Property Name

Description

 

 

 

 

 

 

 

FillPolygon

Used to fill a polygon. This method can be overloaded.

 

 

FillRectangle

Used to fill a rectangle. This method can be overloaded.

 

 

FillRectangles

Used to fill a series of rectangles. This method can be over-

 

 

 

 

 

loaded.

 

 

FillRegion

Used to fill a Region object with the specified color.

 

 

Flush

 

Enforces the execution of pending graphic operations and

 

 

 

 

 

returns immediately without waiting for the operations to

 

 

 

 

 

complete. This method can be overloaded.

 

 

FromHdc

Used to create a new Graphics object from the specified han-

 

 

 

 

 

dle. This method can be overloaded.

 

 

GetHdc

Used to get the handle to the device context of the Graphics

 

 

 

 

 

object.

 

 

IsVisible

Used to indicate whether the pair of coordinates is contained

 

 

 

 

 

within the visible clip region of the Graphics object. This

 

 

 

 

 

method can be overloaded.

 

 

MeasureString

Used to measure a string. This method can be overloaded.

 

 

MultiplyTransform

Used to multiply the world transformation of the Graphics

 

 

 

 

 

object and the Matrix object.

 

 

ReleaseHdc

Used to release a device context handle.

 

 

ResetTransform

Used to reset the world transformation matrix of the

 

 

 

 

 

Graphics object.

 

 

Restore

Used to restore the state of the Graphics object as the state

 

 

 

 

 

of the GraphicsState object.

 

 

RotateTransform

Used to apply rotation to the transformation matrix of the

 

 

 

 

 

Graphics object. This method can be overloaded.

 

 

Save

 

Used to save the current state of the Graphics object.

 

 

ScaleTransform

Applies scaling operation to the transformation matrix of the

 

 

 

 

 

Graphics object. This method can be overloaded.

 

 

SetClip

Used to set the clipping region of the Graphics object. This

 

 

 

 

 

method can be overloaded.

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

333

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property Name

Description

 

 

 

 

 

 

 

 

 

 

 

 

 

TransformPoints

Used to transform an array of points. This method can be

 

 

overloaded.

 

 

 

 

 

 

TranslateClip

Used to translate the clipping region of the Graphics object.

 

 

This method can be overloaded.

 

 

 

 

 

 

TranslateTransform

Used to prepend translation to the transformation matrix.

 

Finalize

This is a protected method of the Graphics class and can be

 

 

overridden.

 

 

 

 

 

 

 

 

 

 

 

 

 

I have discussed the System.Drawing namespace and the Graphics class. In the next section, I discuss the Brush class.

The Brush Class

The Brush class also belongs to the System.Drawing namespace. This class acts as an abstract base class, and the classes derived from this class are used to fill graphical shapes with specified colors. The Brush class cannot be instantiated. You use the classes derived from the Brush class to create brush objects.

Table 11-5 lists the various methods of the Brush class.

Table 11-5 Methods of the Brush Class

Method Name

Description

Clone

Used to create a copy of the Brush object. However, to do

 

this, you need to override it in a derived class.

CreateObjRef

Inherited from the MarshalByRef object and used to cre-

 

ate an object required to generate a proxy.

Equals

Used to determine whether two object instances are equal.

 

This method can also be overloaded.

GetHashCode

Used to serve as a hash function in hashing algorithms.

GetLifetimeService

Inherited from the MarshalByRefObject and used to

 

retrieve the current lifetime service object.

GetType

Used to get the Type for the current instance.

continues

334 Project 2 CREATING AN APPLICATION USING MANAGED C++

Table 11-5 (continued)

Method Name

Description

InitializeLifetimeService

Inherited from the MarshalByRef object and is used to

 

obtain a lifetime service object.

ToString

Used to return a String.

Finalize

This is a protected method of the Brush class and is over-

 

ridden.

MemberwiseClone

Creates a shallow copy for the current Object.

 

 

Next, I discuss the Pen class.

The Pen Class

The Pen class also belongs to the System.Drawing namespace. This class is used to define an object that will be used to draw lines and curves. The Pen class cannot be inherited.

A Pen object can then be used to draw lines of a specified style. You can then use a variety of fill styles to fill the lines. However, the fill style will depend on the brush that is used for filling the line.

The properties of the Pen class are listed in Table 11-6.

Table 11-6 Properties of the Pen Class

Property Name

Description

Alignment

Used to get or set the alignment for the Pen object.

Brush

Used to get or set the Brush object.

Color

Used to get or set the color of the Pen object.

CompoundArray

Used to get or set the array values for the specified pen.

DashCap

Used to get or set the cap style to be used at the end of the

 

dashes when drawing dashed lines.

DashOffSet

Used to get or set the distance to the start of a dash

 

pattern.

DashPattern

Used to get or set custom dashes.

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

335

 

 

 

 

 

 

Property Name

Description

DashStyle

Used to get or set the style for dashed lines.

EndCap

Used to get or set the cap style at the end of the line.

PenType

Used to retrieve the style of a line.

StartCap

Used to get or set the cap style at the start of the line.

Transform

Used to get or set the geometric transformation for the Pen

 

object.

Width

Used to get or set the width of the Pen object.

 

 

Table 11-7 lists the various methods of the Pen class.

Table 11-7 Methods of the Pen Class

Method Name Description

Clone

Used to create an exact copy of the Pen object.

CreateObjRef

Creates an object that generates a proxy. This method is

 

inherited from the MarshalByRefObject.

Dispose

Used to release all the used resources.

Equals

Used to determine whether two Object instances are equal

 

or not.

GetHashCode

Used to serve as a hash function for hashing algorithms.

MultiplyTransform

Used to multiply the transformation matrix with the speci-

 

fied Matrix.

ResetTransform

Used to reset the geometric transformation matrix.

RotateTransform

Used to rotate the geometric transformation by a specified

 

angle. This property is overloaded.

ScaleTransform

Used to scale the geometric transformation.

SetLineCap

Used to set the value for the style of the cap to be used to

 

end lines.

ToString

Returns a String.

continues

336 Project 2 CREATING AN APPLICATION USING MANAGED C++

Table 11-7 (continued)

Method Name Description

TranslateTransform

Used to translate the geometric transformation.

Finalize

A protected method of the Pen class; can be overridden.

MemberwiseClone

Used to create a shallow copy for the current instance of

 

the Object.

 

 

I have discussed GDI+, including its services, features, and programming model changes. I also discussed the System.Drawing namespace and the various classes that constitute this namespace. In the next section, I discuss events and delegates in managed extension code.

Events and Delegates

Events form the foundation of an object-oriented programming language. An event is a message generated by an object that defines the occurrence of an action. Events can be system generated or user generated. The object that generates an event is known as the event sender and the object that receives events is known as the event receiver. While communicating with events, the event sender class is not aware of the object or method that will receive the event. It uses a pointer-like mechanism that acts as an interface between the event source and the event receiver. A delegate provides this pointer-like mechanism.

A delegate is a class that is responsible for holding references to methods. A delegate is a class with its own signature. A delegate can hold only those methods that match with the signature of the delegate. Therefore, a delegate is similar to a callback or a type-safe function pointer. I will discuss the event-handling functionality of delegates later in the chapter. The next section takes you through an introduction to events.

Introducing Events

Events are of two types: predefined system events used for handling events related to controls and user-defined events. These predefined events are used for handling events when the user performs an action on the controls. Typically, a user interface consists of screens that contain one or more controls associated with it. Users

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

337

 

 

 

 

 

 

can interact with these controls to generate results. Each control has a class associated with it, and the class has a set of attributes and methods to add functionality to the class. For example, the Button class is associated with the Button control and has a Click event that is invoked when the user clicks on the button. To trap an event in an application, you need to write an event handler. The event handler is then linked with the event source. This process of registering an event handler with the event source is known as event wiring. Following is a code snippet illustrating the process of event wiring:

Button * bObj;

bObj=new Button();

bObj->Add(“Exit”,new EventHandler(this,OnExit));

Here, bObj is a variable declared to hold a reference to a button object. OnExit is a method that has been declared as the event handler for the click event of the button. The signature of a typical event handling method is as follows:

void OnExit (Object* sender, EventArgs *e);

You can raise events through code. I discuss raising events in the next section.

Raising Events

To provide event functionality, you require three elements: a class to provide event data, an event delegate, and a class to raise events. An event delegate has two parameters: a reference to the source that raised the event and the data of the event. The event data class is derived from System.EventArgs. There may be situations where the event does not generate data. In such situations, it uses EventArgs as the data type for the event.

The details of event wiring may differ in Windows Forms and Web Forms because of the levels of support provided by RAD tools.

I will now discuss the various important classes and events related to event handling.

The EventHandler Delegate

The functionality of event handling in the .NET Framework revolves around an event delegate and its handler. You require a class to hold event data and a delegate to point to the event handler.

338 Project 2 CREATING AN APPLICATION USING MANAGED C++

The EventHandler delegate represents the method to be used to handle events that have no data. The syntax for the EventHandler delegate is as follows:

public_gc_delegate void EventHandler ( Object* sender, EventArgs* e);

NOTE

The event handler declaration must match the declaration of the EventHandler delegate.

In the preceding syntax, sender is the name of the source for the event and e refers to an EventArgs that consists of the event data.

By creating an EventHandler delegate, you identify the method used to handle the event.

If the event does not generate data, you need to use the EventArgs class for the event data object and the EventHandler as the event delegate. By creating an EventHandler delegate, you identify the method that will be handling the event. You need to attach events with event handlers. To do so, you need to add an instance of the delegate to the event. The event handler will be called when the event occurs.

I will now discuss the EventArgs class.

The EventArgs Class

The EventArgs class belongs to the System namespace. This class acts as the base class for all classes that contain event data. However, this class does not contain any event data. It can be used by events that do not pass any information to their event handlers. If the event handler requires information, the application should derive a class from the EventArgs class to store the data.

The methods of the EventArgs class are listed in Table 11-8.

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

339

 

 

Table 11-8 Methods of the EventArgs Class

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Method

Description

 

 

 

 

 

 

 

 

 

 

 

 

 

EventArgs

This is the EventArgs constructor and is used to initialize an

 

 

 

 

instance of the EventArgs class.

 

 

 

 

 

 

Empty

This is the public field for the EventArgs class. The field

 

 

 

 

represents an event that has no event data.

 

 

 

 

 

 

Equals

This is the public method for the EventArgs class. This

 

 

 

 

method is used to determine whether or not two Object

 

 

 

 

instances are equal. The method can also be overloaded.

 

 

 

GetHashCode

This method is used to serve as a hash function for hashing

 

 

 

 

algorithms.

 

 

 

 

 

 

GetType

This method is used to get the Type for the current instance.

 

 

 

ToString

This method is used to return a string that represents the cur-

 

 

 

 

rent Object.

 

 

 

 

 

 

Finalize

This is the protected method of the EventArgs class.

 

 

 

 

 

 

 

 

 

 

Introducing Delegates

As stated earlier, a delegate is a class that is used to store references to methods. A delegate has a signature. The next section discusses the important classes for delegates.

The Delegate Class

This class belongs to the System namespace and is used as a base class for all other delegate types. However, only the system and compilers are permitted to derive a new type from this class.

NOTE

This class should not be considered as a delegate type. Instead, you can derive delegate types from this class.