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

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

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

In the previous chapter, you learned about handling events in MFC code. You will now learn about handling events in managed code. In this chapter, you will learn about GDI+, events, and delegates in a Managed Extensions application.

 

Introducing GDI+

 

 

 

Y

 

GDI+ is an extension of GDI and is theLgraphical device interface for Microsoft

 

Windows XP. Just like GDI, GDI+ is also used to display information on either

 

 

 

 

 

F

 

a VDU or a printer and can be used by developers to create device-independent

 

applications. GDI+ is an PI that is exposed through a set of classes organized as

 

 

 

M

 

 

managed code. his set of classes is known as the managed class interface to GDI+.

 

 

A

 

 

 

E

 

A GDI, such as a GDI+, helps developers

 

MANAGED CLASS INTERFACE

 

 

 

to display information on a screen or a

 

T

 

 

printer without knowing about a particular

 

The managed class interface to GDI+

 

 

 

 

 

display device. The main goal of GDI+ is to

 

consists of 60 classes, 50 enumerations,

 

 

 

 

 

improve the graphical desktop experience

 

and 8 structures. The mainstay of the

 

 

 

 

 

for users. GDI+ allows integration of 2-D

 

managed class interface is the Graphics

 

 

 

class that is used to draw lines, curves,

 

 

and 3-D graphics, permits digital imaging,

 

figures, etc. A number of other classes

 

 

and provides hardware acceleration with

 

can work in conjunction with the Graphics

 

 

good visual quality. It also offers improved

 

 

 

 

 

graphics capabilities, such as enhanced typography and imaging. The developer just needs to call the various methods of the GDI+ classes. These methods, in turn, call the specific device drivers. Another advantage of GDI+ is that it protects an application from the application hardware, thus allowing developers to create device-independent applications.

As discussed above, GDI+ offers a number of services such as imaging and typography. In the next section, I discuss these services.

The Services Offered by GDI+

The services offered by GDI+ fall into the following broad categories:

Team-Fly®

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

321

 

 

 

 

 

 

Imaging. Certain pictures, such as pictures displayed as icons, cannot be represented as a series of lines and curves. The same is applicable for high-resolution pictures. To store such images, you need to store them as bitmaps. The Bitmap class of GDI+ can be used to display and save such bitmaps.

2-D and 3-D vector graphics. GDI+ allows 2-D and 3-D vector graphics. Vector graphics are used to draw lines and curves that can be represented by a set of points on a coordinate system. For example, to specify a rectangle, you can specify its upper-left corner and a pair of numbers giving its width and height. GDI+ consists of various classes that store information about the lines and curves and perform the actual drawing. For example, the Rectangle class can be used to store the information about a rectangle, and the Pen class can be used to store information about the line color, width, style, etc.

You can use a metafile to record a vector image. The Metafile class in GDI+ can be used to record and save a metafile. Other classes related

to the Metafile class, such as the MetafileHeader and the MetaHeader,

can be then used to monitor the data stored in the metafile header.

NOTE

A vector image is nothing but a series of graphics commands.

Typography. Typography deals with the display of information in different fonts, and styles. GDI+ supports a range of features that ensure typography.

Having discussed the various services offered by GDI+, I will now discuss the new features of GDI+ in the following section.

New Features of GDI+

As stated earlier, GDI+ is an extension of GDI. However, GDI+ is different from GDI in a number of ways. The various new features in GDI+ are as follows:

Linear and horizontal gradient brushes

322Project 2 CREATING AN APPLICATION USING MANAGED C++

Independent path objects

Cardinal splines

The Matrix object

Transformations to regions

Alpha blending

Support for different image formats

I will now discuss each one of them in detail.

Linear and Horizontal Gradient Brushes

GDI+ provides the linear gradient brush and the path gradient brush for filling regions of different styles. You can also use these brushes to fill a shape with color that changes as you move the brush across the shape. For example, you can create a horizontal gradient brush by specifying yellow at the left edge of a shape and green at the right edge of the shape. As you fill the shape by moving the brush from left to right, the color of the shape will change gradually from yellow to green. You can also create a vertical gradient brush in the same manner. The color of the shape will change as you move the brush from top to bottom.

Similarly, you may want to change the color of a shape as the brush is moved from the edges toward the center or vice versa.

Independent Path Objects

A path is a sequence of connecting straight lines. Every path belongs to a device context in GDI. A path is destroyed as it is drawn. You use the Graphics object in GDI+ to perform drawing. You can also create several GraphicsPath objects. A GraphicsPath object is not destroyed after the drawing action; therefore, you can use the same GraphicsPath object to draw a path umpteen number of times.

Cardinal Splines

A cardinal spline is a set of individual curves joined to form a bigger curve. A spline is therefore specified as an array of points. A cardinal spline traverses smoothly through each point in the array and is therefore more polished than a path created by connecting straight lines.

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

323

 

 

 

 

 

 

The Matrix Object

You can use the Matrix object in GDI+ to make transformations of objects simple. You can use the Matrix object in conjunction with the objects that need transformation.

Transformations to Regions

A region is a part of a display area. In GDI, the only transformation that you can apply to a region is to translate it. However, things are a little different in GDI+. You can apply a range of transformations in GDI+ such as scaling, rotating, and translating. GDI stores the regions in device coordinates whereas GDI+ stores regions in world coordinates.

Alpha Blending

Another feature of GDI+ that you can use to work with graphics is to specify the transparency of a fill color. This method is known as alpha blending. The transparency of a fill color refers to the degree by which the background color is visible through the fill color. In alpha blending, a transparent color is blended with the background color. You can also specify different levels of transparency to colors.

TIP

The more transparent is the fill color, the more the background color is visible.

Support for Different Image Formats

GDI+ supports a variety of images, such as BMP, GIF, JPEG, EMF, PNG, TIFF, ICON, and so on, through the Image, Bitmap, and Metafile classes. These classes help you save and manipulate images of different formats.

I have discussed the new features of GDI+ as compared to GDI. In the next section, I discuss the various changes in the programming model in GDI+.

324 Project 2 CREATING AN APPLICATION USING MANAGED C++

Programming Model Changes

Before proceeding with the changes with respect to programming in GDI+, first recall the programming structure in GDI. In GDI, to work with the various GDI functions, you first need to obtain the handle for a device context and then pass this handle as an argument to the various GDI functions. These GDI functions are responsible for the actual drawing.

NOTE

A device context is a structure that is used by Windows to store information about display devices. Device contexts are also used to specify the attributes that specify the manner in which items will be drawn on the display devices.

In GDI+, you do not need to pass handles or device contexts to the GDI functions. Instead, you can use the Graphics object and call its methods directly. GDI+ uses the object-oriented model, whereas GDI uses the handle-based programming model. The Graphics object is the foundation of GDI+ just as the device context in GDI. Although the two perform the same roles, there are a few differences as well. The device context in GDI is always associated with a GDI object, such as a pen, brush, etc. To use a device context in GDI to fill a shape with color, you first need to call the SelectObject method to associate a Brush object with the device context. All shapes using this device context will be filled with the same color until you select a brush with a different color. However, in GDI+ you can call the FillPolygon method of the Graphics class to fill a polygon. You can have different FillPolygon calls and do not need to associate a Brush object with a Graphics object.

You must be aware of the concept of overloading, because many functions in GDI+ are overloaded. The next section discusses overloading methods in GDI+.

Overloading Methods in GDI+

Method overloading refers to methods (functions) having the same name but different functionality. These methods differ by their signature that includes the number of parameters, the type of parameters, and the sequence of the parame-

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

325

 

 

 

 

 

 

ters. For example, the FillPolygon method of the Graphics class can take the following forms:

Overloads Public Sub FillPolygon (Brush, Point())

Overloads Public Sub FillPolygon (Brush, PointF())

Overloads Public Sub FillPolygon (Brush, Point(), FillMode)

Overloads Public Sub FillPolygon (Brush, PointF(), FillMode)

Different Methods for Drawing and Filling

GDI+ offers a range of solutions for drawing the outlines and filling the shape. Consider an example where you want to draw and fill a rectangle. In GDI, you can directly use the Rectangle function that takes care of drawing and filling the rectangle with the specified color. The rectangle is drawn with the currently selected pen and the rectangle is filled with the currently selected brush.

In GDI+, you need to use two separate functions of the Graphics class, DrawRectangle and FillRectangle, for drawing the outline of a rectangle and filling the rectangle, respectively. One of the arguments of the DrawRectangle method, the Pen object, is used to draw the outline of the rectangle. Similarly, one of the arguments of the FillRectangle method, the Brush object, is used to fill the rectangle.

Creating Regions

You can use the various functions provided by GDI+, such as CreateRectRgn,

CreateEllpticRgn, CreateRoundRectRgn, CreatePolygonRgn, and CreatePoly-

PolygonRgn, to create regions. The Region class provides a constructor that can receive a Rectangle object and a GraphicsPath object. Consider a situation where you want to construct a region based on a polygon. To do this, you need to create a GraphicsPath object that contains the polygon and pass this GraphicsPath object to the Region constructor.

You can also combine different shapes and paths to create complex regions by using the Union and Intersect methods of the Region class. An important point to note here is that the GraphicsPath object is not destroyed after it is passed to the Region constructor or when it is passed as an argument to the Union or Intersect method. However, in GDI the path gets destroyed when you convert a path to a region by using the PathToRegion function. Therefore, the same path can be used as a building block for different regions in GDI+.

326 Project 2 CREATING AN APPLICATION USING MANAGED C++

I have discussed the various programming model changes in this section. At this stage, it is now important for you to learn about the different classes and namespaces in GDI+.

Important Classes and Namespaces in

GDI+

The following is a list of classes and namespaces that will be described in this section:

The System.Drawing namespace

The Graphics class

The Brush class

The Pen class

I will now discuss each of them in detail.

The System.Drawing Namespace

This namespace is used to provide access to the basic graphics functionality in

GDI+. The System.Drawing.Drawing2D, System.Drawing.Imaging, and Sys-

tem.Drawing.Text namespaces can be used to provide advanced functionality. The classes that constitute the System.Drawing namespace are listed in Table 11-1.

Table 11-1

Classes in the System.Drawing Namespace

Class

Description

 

 

Bitmap

Encapsulates a bitmap that consists of the pixel data for a

 

graphics object. (Recall that a Bitmap object is an object that

 

is used to work with images defined by pixel data.)

Brush

Acts as a base class. Classes derived from this class can be used

 

to define objects that can fill the various graphical shapes with

 

specified colors.

Brushes

Specifies the brushes for all the standard colors. This class

 

cannot be inherited.

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

327

 

 

 

 

 

 

Class

Description

ColorConverter

Used to convert colors from one data type to another. You can

 

access this class from the TypeDescriptor object.

ColorTranslator

Used to translate colors from the Color structures in GDI+.

 

This class cannot be inherited.

Font

Used to define a particular format for text. This class cannot

 

be inherited.

FontConverter

Used to convert the Fonts objects from one data type to

 

another. You need to access the FontConverter class through

 

the TypeDescriptor object.

FontFamily

Defines a group of typefaces that share common basic design.

 

This class cannot be inherited.

Graphics

The most important class of GDI+ and constitutes the GDI+

 

drawing surface. This class cannot be inherited.

Icon

Used to represent a Windows icon. (Icons can be considered as

 

small bitmap images whose size is determined by the system.)

IconConverter

Used to convert an Icon object from one data type to another.

 

You need to access this class from the TypeDescriptor

 

object.

Image

Provides functionality to the Bitmap, Icon, and Metafile

 

classes.

Pen

Used to define an object that can be used to draw lines and

 

curves. This class cannot be inherited.

Pens

Defines pens for all the standard colors. This class cannot be

 

inherited.

PointConverter

Used to convert a Point object to different data types.

RectangleConverter

Used to convert a rectangle to different data types. You can

 

access this class from the TypeDescriptor object.

Region

Used to describe the interior of a graphics shape. This class

 

cannot be inherited.

SizeConverter

Used to convert to different data types. You can access this

 

class from the TypeDescriptor object.

continues

328

Project 2

CREATING AN APPLICATION USING MANAGED C++

 

 

 

 

 

 

 

 

Table 11-1 (continued)

 

 

 

 

 

 

 

 

 

Class

 

Description

 

 

 

 

 

 

 

SolidBrush

Used to define a brush. This class cannot be inherited.

 

 

StringFormat

Defines the text layout and display information. This class

 

 

 

 

 

cannot be inherited.

 

 

SystemBrushes

Each property of this class is a SolidBrush object.

 

 

SystemIcons

Each property of this class is an Icon object. This class cannot

 

 

 

 

 

be inherited.

 

 

SystemPens

Each property of this class is a Pen object.

 

 

TextureBrush

Each property of this class is a Brush object. This class cannot

 

 

 

 

 

be inherited.

 

 

ToolboxBitmapAttribute

Consists of objects that define the images for a specified

 

 

 

 

 

component.

 

 

 

 

 

 

The System.Drawing namespace also consists of structures in addition to classes. The structures in the System.Drawing namespace are listed in Table 11-2.

Table 11-2 Structures in the System.Drawing Namespace

Structure Name

Description

CharacterRange

Defines a range of character positions.

Color

Represents an RGB color.

Point

Represents an ordered pair of integer coordinates that defines

 

a point in a two-dimensional space.

PointF

Represents an ordered pair of float coordinates.

Rectangle

Used to store the information for a rectangular region.

RectangleF

Used to store the information for a rectangular region in float-

 

ing point.

Size

Used to represent the size for a rectangular region.

SizeF

Used to represent the size for a rectangular region in floating

 

point.

 

 

INTRODUCTION TO GDI+, EVENTS, AND DELEGATES

Chapter 11

 

329

 

 

 

 

 

 

The Graphics Class

The Graphics class is the most important class of GDI+. This class forms the core of GDI+. The Graphics class belongs to the System.Drawing namespace and encapsulates the GDI+ drawing surface. The Graphics class consists of methods that are used for drawing objects. You cannot inherit the Graphics class. The various properties of the Graphics class along with their description are listed in Table 11-3.

Table 11-3 Properties of the Graphics Class

Property Name

Description

Clip

Used to get or set a Region object.

CompositingMode

Used to get a value that specifies the manner in which the

 

composited images are drawn to the Graphics object.

CompositingQuality

Used to get or set the quality of the composited images that

 

are drawn to the Graphics object.

DpiX

Used to get the horizontal resolution of the Graphics object.

DpiY

Used to get the vertical resolution of the Graphics object.

InterpolationMode

Used to get or set the interpolation mode of the Graphics

 

object.

IsClipEmpty

Used to receive a value that specifies whether the clipping

 

region of the Graphics object is empty.

IsVisibleClipEmpty

Used to receive a value that specifies whether the visible clip-

 

ping region of the Graphics object is empty.

PageScale

Used to work with the scaling between the world units and

 

the page units for the Graphics object.

PageUnit

Used to get or set the unit of measure for the page coordinates

 

in the Graphics object.

RenderingOrigin

Used to get or set the rendering origin of the Graphics

 

object.

SmoothingMode

Used to get or set the rendering quality for the Graphics

 

object.

continues