Using the System.Drawing Namespace

Using the System.Drawing Namespace

Once a form can respond to events, you can consider how the form can make use of graphical interface elements. This is accomplished using the System.Drawing namespace, included in the Windows Graphics Design Interface (GDI+). GDI+ includes the following four namespaces in the file System.Drawing.dll :

  • System.Drawing Provides access to basic graphics functionality

  • System.Drawing.Drawing2D Provides advanced two-dimensional and vector graphics

  • System.Drawing.Imaging Handles images

  • System.Drawing.Text Manages typographical functions

For most simple graphical operations, you'll work directly with the System.Drawing.Graphics class. In the following sections, I'll show you how to use this class.

The Graphics Class

The Graphics class within the System.Drawing namespace is referred to as a sealed class . It cannot be directly inherited like the Form class but rather must be instantiated through one of the following methods :

  • Through a PaintEventArgs argument passed to the Paint event handler of a control or a form. The resultant Graphics object represents the drawing surface of the object that called the event.

  • By calling the CreateGraphics method of a control or form.

  • By calling the Graphics.FromHwnd method and passing to it the current form.

  • By calling the Graphics.FromImage method. This method will take an image object and return a Graphics object corresponding to that image. You can use the resultant Graphics object to manipulate the image.

Structures in the System.Drawing Namespace

Within the System.Drawing namespace are several structures that may be used to manipulate Graphics objects. Table 1.7 lists those you should be familiar with.

Table 1.7. The System.Drawing Namespace Structures

Structure

Description

CharacterRange

A range of character positions within a string.

Color

The Color structure has 140 static properties, each representing the name of a color as well as the four properties A, R, G , and B , specifying the Alpha, Red, Green, and Blue portions of the color. A Color value can be created using the static constructor FromArgb, FromKnownColor , or FromName .

Point

An ordered pair of integer x and y that defines a point in a two-dimensional plane, including a set of methods and operators to work with Point .

PointF

A float version of the Point structure.

Rectangle

The integer location and size of a rectangular region, created using a Point and Size . In the Rectangle 's constructor, a Point object represents the top-left corner, and a Size object specifies the region's width and length from the origin.

RectangleF

A float version of the Rectangle structure.

Size

The integer size of a rectangular region (width and height).

SizeF

A float version of the Size structure.

graphics/note_icon.gif

The size of a form is measured from its top-left corner, which serves as its origin (0,0). The client area of a form is the portion excluding the title bar, borders, and a menu (if present). The value of x increases as you measure to the right, and the value of y increases as you measure downwards.


Drawing Text on a Form

In order to draw text on a Windows form, use the DrawString method of the Graphics class. This takes the following form:

 Public Sub DrawString(string, Font, Brush, Single, Single) 
graphics/alert_icon.gif

GDI+ and the Windows Forms Library include full support for the Unicode character set, making it possible to draw text in any language supported by the system.


This form of the DrawString method accepts the following arguments:

  • The first argument is the string to be displayed.

  • The second argument is the font of the string.

  • The third argument is the type of brush. The Brushes enumeration provides you with a variety of Brush objects, each with a distinct color.

  • The fourth and fifth arguments are used to specify the x and y location for the point that marks the start of the string on the form. Both of these values are required to be of the Single type.

Using the Invalidate Method

The Paint event regenerates the Graphics object when the Show method is called. In order to cause the Graphics object to be redrawn during other events, such as a form Resize action, include the following code in the form:

 Private Sub ResizeRedraw_Paint(ByVal sender As Object, _  ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     Dim grfx As Graphics = e.Graphics     Dim strText As String = String.Format( _      "Form Size is: Width={0}, Height={1}", Width, Height)     grfx.DrawString(strText, Font, Brushes.Black, 0, 0) End Sub 

You should also add the event handler for the form's Resize event to call this code, like this:

 Private Sub ResizeRedraw_Resize(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles MyBase.Resize     Invalidate() End Sub 

The Invalidate() method is called without any arguments in this example to cause a Paint regeneration of the entire form. You may also provide a Rectangle parameter in order to restrict the Paint regeneration to less than the entire form when the Invalidate() method is called.

Using the ResizeRedraw Property

You may also specify that the Graphics items on the entire form be regenerated during a form Resize event by setting the ResizeRedraw property to True within the form's constructor.

Drawing Shapes Using the Draw Method

It is often desirable to draw shapes and graphics on a form. The Graphics class allows you to draw many shapes. Table 1.8 lists the drawing methods of the Graphics class and the shapes you can create with them.

Table 1.8. Drawing Methods from the Graphics Class

Method

Description

DrawArc

An arc that represents portion of an ellipse

DrawBezier

A B zier curve defined by four points

DrawBeziers

A series of B zier curves

DrawClosedCurve

A closed curve defined by an array of points

DrawCurve

A curve defined by an array of points

DrawEllipse

An ellipse defined by a bounding rectangle specified by a pair of coordinates, a height, and a width

DrawIcon

The image represented by the specified Icon object located at the given coordinates

DrawImage

An Image object at the specified location, preserving its original size

DrawLine

A line connecting two points

DrawLines

A series of line segments that connect an array of points

DrawPath

A GraphicsPath object

DrawPie

A pie shape defined by an ellipse and two radial lines

DrawPolygon

A polygon defined by an array of points

DrawRectangle

A rectangle specified by a point, a width, and a height

DrawRectangles

A series of rectangles

DrawString

Draws the given text string at the specified location with the specified Brush and Font objects

graphics/alert_icon.gif

In order to be able to access the graphical drawing methods of the Graphics class, you must include this line of code:

 Imports System.Drawing.Drawing2D 

The various Draw methods make use of several arguments, including the Pen class and the SmoothingMode property. You can further refine the drawing output by customizing the properties present in the Pen class.

Table 1.9 lists several Pen - related classes within the System.Drawing namespace, and Table 1.10 lists some of the properties of the Pen class you should be familiar with.

Table 1.9. Classes Related to Pen in the System.Drawing Namespace

Class

Description

Pen

An object used to draw lines and curves.

Pens

Provides 140 static properties, each representing a Pen of a standard color.

SystemPens

Provides a set of static properties named after a Windows display element. Each of these properties returns a Pen object representing the color of the element with a width of 1.

Table 1.10. Properties of the Pen Class

Property

Description

Alignment

The alignment for the Pen object

Brush

A Brush object that determines attributes of the Pen object

Color

The color of the Pen object

DashCap

The cap style used at the end of the dashes in dashed lines

DashPattern

An array of custom dashes and spaces

DashStyle

The style used for dashed lines

EndCap

The cap style used at the end of lines

LineJoin

The join style for the ends of two consecutive lines

PenType

The style of lines

StartCap

The cap style used at the beginning of lines

Width

The width of the Pen object

Table 1.11 details the enumerated values available as values of the SmoothingMode property. Antialiasing is the process where semitransparent pixels are included at the boundaries of two colors in order to create a smoother appearing boundary.

Table 1.11. The SmoothingMode Enumeration Members

Member Name

Description

AntiAlias

Antialiased rendering.

Default

Same as None .

HighQuality

High-quality, low-performance rendering. Same as AntiAlias .

HighSpeed

High-performance, low-quality rendering. Same as None .

Invalid

Invalid mode. Raises an exception.

None

Does not use antialiasing.

Creating Filled Shapes Using the Fill Method

An additional option for the creation of graphical objects on a form includes the use of the Fill method in order to create objects that are colored in a nonuniform shade . Table 1.12 details the Fill methods of the Graphics class.

Table 1.12. The Fill Methods of the Graphics Class

Method

Description

FillClosedCurve

Fills the interior of a closed curve defined by an array of points

FillEllipse

Fills the interior of an ellipse defined by a bounding rectangle

FillPath

Fills the interior of a GraphicsPath object

FillPie

Fills the interior of a pie section defined by an ellipse and two radial lines

FillPolygon

Fills the interior of a polygon defined by an array of points

FillRectangle

Fills the interior of a rectangle specified by a point, a width, and a height

FillRectangles

Fills the interiors of a series of rectangles

FillRegion

Fills the interior of a Region object

The various Fill methods include a greater capability for filling shapes with patterns or shaded color gradients than the Draw method provides. The Fill method uses Brush objects in order to determine the details of the fill that will occur. Table 1.13 lists the classes involved in the creation of Brush objects.

Table 1.13. The Brush-Related Classes in the System.Drawing and System.Drawing.Drawing2D Namespace

Class

Description

Brush

An abstract base class used to create brushes, such as SolidBrush, TextureBrush , and LinearGradientBrush . These are used to fill the interiors of graphical shapes such as rectangles, pies, polygons, and paths.

Brushes

Contains 140 static properties for the names of every standard color.

HatchBrush

Used to fill a region using one of a large number of patterns available in HatchStyle .

LinearGradientBrush

Used to create two-color gradients and multicolor gradients. The default is a linear gradient from one color to another along a specified line.

SolidBrush

Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, pies, polygons, and paths.

SystemBrushes

A set of properties named after a Windows display element. Each of these properties returns a SolidBrush object representing the color of the display element.

TextureBrush

A Brush object that uses an image to fill the interior of a shape.

In this chapter, you learned about the basic architecture of .NET, including the Common Language Runtime and the Framework class library. You saw your first Visual Basic .NET project and learned how to create events for a form. You also learned about some of the important classes that can be used to dress up forms with graphical information. The next chapter looks at form user controlstheir attributes, properties, and methods for adding them to forms.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net