GDI Architecture

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 11.  Introduction to GDI+


GDI+ Architecture

To program with GDI+, you need to understand a few basic concepts, and you must be familiar with how the key classes are organized into certain .NET namespaces. This section outlines the logical structure of GDI+ and the organization of the most important GDI+ classes. We begin with the basic concept of painting.

Windows Forms Painting

Although not directly part of GDI+, an important part of the story of drawing in Windows is the painting architecture of Windows Forms. The relevant classes and structures are in the Windows.Forms namespace.

The base class for objects with a visual representation is Control , which implements basic functionality needed for displaying information to the user. This class handles user input via the keyboard and mouse, and it routes messages by raising events. It also provides many overridable methods . Most Windows applications make use of the Form derived class.

Paint Events

The OnPaint method is an overridable method of the Control class, which is called whenever the control needs repainting . The base class's implementation raises the Paint event, which can be handled by any delegate attached to the event. A derived class may perform needed painting either by handling the Paint event or by overriding the OnPaint method. The latter is preferred. When overriding OnPaint , it is important to call the base class's OnPaint method so that Paint events will be raised and registered delegates can receive the event.

The program PaintDemo\Version 1 illustrates painting both by overriding OnPaint and handling the Paint event. Build and run the program. You will see that one message is displayed in response to the override of OnPaint , and a second message is displayed by handling the Paint event.

Here is the code. Note that we have also overridden OnLoad for the purpose of setting the background color to white (which we could also have accomplished by setting the BackColor property in the Property window for the Form). We will discuss the drawing code and the use of the Graphics class a little later in the chapter. (For convenience, we use the Windows Forms designer throughout this chapter, and so all our code examples contain Forms designer support.)

 graphics/codeexample.gif Public Class Form1    Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " ...  Protected Overrides Sub OnPaint(_   ByVal e As System.Windows.Forms.PaintEventArgs)  Dim g As Graphics = e.Graphics  g.DrawString("Overriding On Paint", Font, _   Brushes.Black, 25, 25)   MyBase.OnPaint(e)  End Sub  Private Sub Form1_Paint(ByVal sender As Object, _   ByVal e As System.Windows.Forms.PaintEventArgs) _   Handles MyBase.Paint  Dim g As Graphics = e.Graphics  g.DrawString("Handling Paint Event", Font, _  Brushes.Black, 50, 50)    End Sub    Protected Overrides Sub OnLoad(_     ByVal e As System.EventArgs)       Me.BackColor = Color.White  MyBase.OnLoad(e)  End Sub End Class 

The DrawString method of the Graphics class is used for drawing a text string and has several overloaded versions. The version used in this code takes a string as the first parameter, a font as the second parameter, and a brush as the third parameter. The remaining parameters are coordinates that specify the location of the upper-left corner of the drawn text.

The call to MyBase.OnPaint at the bottom of the OnPaint code is important, enabling the Paint event to be raised. As an experiment, you can try commenting out this call. You will see that only the message from OnPaint is displayed. For similar reasons, you should call the base class's OnLoad at the bottom of your derived class's override of OnLoad , enabling the Load event to be raised.

PaintEventArgs

Both the overridden OnPaint method and the Paint event handler take an argument that is an object of the PaintEventArgs class. This class has two read-only properties:

  • Graphics , which specifies the Graphics object on which to paint.

  • ClipRectangle , which specifies the rectangle in which to paint. Any drawing done outside this rectangle will be "clipped" and will not be shown on the drawing surface.

The clipping rectangle can make painting more efficient. We will return to this topic later in the chapter.

On-Demand Painting

The way painting works in Windows (and in all other graphical user interface systems) is "on demand." Thus, if a window is covered up and then uncovered, a Paint event will be raised as a signal that the window must be repainted.

It is perfectly possible to perform drawing commands at other points in your program, and we will see some examples later in the chapter. PaintDemo\Version 2 illustrates drawing a third message when the mouse button is clicked. All we do is provide a handler for the MouseDown event. The third message is displayed at the location where the mouse was clicked. If you repeatedly click the mouse, you will see messages displayed at each point where the mouse was clicked, as illustrated in Figure 11-2.

Figure 11-2. Main window shows overriding OnPaint and handling Paint and MouseDown events.

graphics/11fig02.jpg

But if you cover up the mouse event messages, they will disappear, unlike the paint messages, which will be always redisplayed.

Here is the code of the mouse event handler:

 graphics/codeexample.gif Private Sub Form1_MouseDown(ByVal sender As Object, _  ByVal e As System.Windows.Forms.MouseEventArgs) _  Handles MyBase.MouseDown    Dim g As Graphics = Me.CreateGraphics    g.DrawString("Handling Mouse Event", Font, _            Brushes.Black, e.X, e.Y) End Sub 

The drawing code involving the DrawString method is the same as we used elsewhere in the program. What is different is how we obtain the Graphics object, using the CreateGraphics method, inherited from the Control class.

Now it is time to begin our study of the Graphics class.

Graphics Class

The heart of GDI+ is the Graphics class in the System.Drawing namespace. It is the class that has methods to draw text, lines, rectangles, and other shapes . A Graphics object is associated with a particular display device. Thus, the actual target of a drawing operation depends on the particular Graphics object used; the drawing code itself is the same.

An example of drawing with the Graphics class was provided in the PaintDemo program, where we displayed a text string using the DrawString method.

 Dim g As Graphics = e.Graphics g.DrawString("Overriding On Paint", Font, _    Brushes.Black, 25, 25) 

The DrawString method is typical of the drawing methods of the Graphics class. The method call contains information about what is to be drawn (a string, in this case), how it is to be displayed (a font and a brush), and where the output is to be located ( x and y coordinates). These calls are stateless and do not rely on information that is stored in the Graphics object and is remembered from call to call. This model is different from the older GDI, in which the device context , which is the analog of the GDI+ Graphics object, preserved state such as the font and brush to be used in a drawing operation. The new model is more straightforward and easier to program.

Drawing Methods

Table 11-1 shows some of the common drawing methods of the Graphics class. This list is not exhaustive, and you should consult the .NET Framework class library reference documentation for a complete list and for details.

Table 11-1. Common Drawing Methods of the Graphics Class
Drawing Method Description
DrawArc Draws an arc representing a portion of an ellipse
DrawBezier Draws a Bzier spline
DrawEllipse Draws an ellipse defined by a bounding rectangle
DrawImage Draws an image
DrawImageUnscaled Draws an image with no scaling
DrawLine Draws a line connecting two points
DrawPie Draws a pie shape
DrawPolygon Draws a polygon defined by an array of points
DrawRectangle Draws a rectangle
DrawString Draws a text string
FillEllipse Fills the interior of an ellipse
FillPolygon Fills the interior of a polygon
FillRectangle Fills the interior of a rectangle
Other Functionality

The Graphics class supports a number of additional functions besides a wide selection of drawing methods. There are properties to give read-only access to characteristics of the associated device, such as the horizontal and vertical resolution. Coordinate transformations of various sorts are supported. A clipping region is used to more efficiently control where output is allowed to go on a drawing surface.

Pens and Brushes

The Graphics class represents the drawing surface and provides drawing methods. When you draw, you need to make use of drawing tools, such as pens and brushes, which are defined by their own classes in the .NET Framework. Most of these classes are in the System.Drawing namespace, but some classes are in the System.Drawing.Drawing2D namespace.

The Pen class defines objects that are used to draw lines and curves. A pen has a width and a color (or in fact can have an associated brush).

There are several brush classes, which are used for drawing the interior of a shape. A brush has a color and may have a pattern.

  • The SolidBrush class defines brushes of a single color.

  • The HatchBrush class defines brushes that have a hatch style, a foreground color, and a background color.

  • The LinearGradientBrush and PathGradientBrush classes define gradient brushes that can change color gradually as you move across the shape.

  • The TextureBrush class defines brushes that can be used to fill a shape based on a pattern stored in a bitmap.

The hatch and gradient brush classes are in the System.Drawing.Drawing2D namespace.

Text and Fonts

In a graphical user interface system, text data is treated as another kind of graphic, of a particularly complex sort . A font is used to render text data to a drawing surface. The Font class defines how text is to be formatted when rendered, including a font face, a size , and a style. The Graphics class has methods to measure a string so that you can properly position strings when you draw.

Images, Bitmaps, and Metafiles

The Bitmap class can be used for working with raster images, where a picture is stored as an array of pixels. The Metafile class can be used for storing drawing commands, which can be used to define vector images. Both Bitmap and Metafile inherit from the Image class. Methods are provided that make it easy to save and restore bitmaps and metafiles. Images can be manipulated by a number of methods, and they can be displayed.

Coordinates

GDI+ defines three kinds of coordinates:

  • World coordinates are logical coordinates that are used in drawing operations.

  • Page coordinates are relative to the upper left corner of a client area.

  • Device coordinates are page coordinates specified in a particular unit of measurement. If the unit is pixels, device and page coordinates are the same.

The Graphics class has properties and methods that support coordinate transformations. A world transform converts world coordinates to page coordinates through methods such as RotateTransform , ScaleTransform , and TranslateTransform . A page transform converts page coordinates to device coordinates, using the PageUnit and PageScale properties of the Graphics object.

Coordinates are important in scrolling, which changes the world coordinates with respect to the page coordinates. The Form class has properties (inherited from the ScrollableControl class) that support "automatic" handling of scrolling.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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