GDI Structures

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+ Structures

In order to program using GDI+, you need to use a number of simple structures that define points, rectangles, sizes, and colors. These are quite straightforward, but their use involves a few details, such as the existence of both integer and floating-point versions, converting among the structures, and so forth. In this section we summarize key features of these structures.

Coordinates and sizes can be expressed as either integer or floating-point numbers . Some methods use one type, and other methods use the other type, so you need to be familiar with both. Integer structures are Point , Size , and Rectangle . Floating-point structures have the same names with an "F" suffix, so they are PointF , SizeF , and RectangleF . Operators are overloaded in a natural way so that you can test for equality by using the equality operator and so forth.

Point and PointF

The Point structure represents an ordered pair of integers that defines a point in a two-dimensional plane. There are properties X and Y representing the x-coordinate and y-coordinate respectively. A constructor can create a point from two integers.

 Dim p as Point = new Point(5, 15) 

The PointF structure is the same, except it represents a pair of floating-point numbers. Since an Integer can be implicitly converted to a Single , you could convert a Point to a PointF by constructing a new PointF out of the X and Y members of the PointF .

 Dim p As New Point(5, 16) Dim pf = New PointF(p.X, p.Y) 

For converting in the opposite direction, you can use the static methods Round , Truncate , or Ceiling of the Point class.

 Dim pf As New PointF(5.3, 16.7) Dim p As Point = Point.Round(pf) 

Size and SizeF

A Size is also an ordered pair of integers, but it is used to represent the size of a rectangular region rather than a location. It has properties Width and Height . The SizeF structure is similar, with an ordered pair of floating-point numbers. Conversion issues are handled in the same manner as with Point and PointF .

Rectangle and RectangleF

A Rectangle is a combination of a Point and a Size . Alternatively, it can be defined as four integers. The first two represent the location of the top-left corner, and the other two represent the width and height. There is a constructor corresponding to each definition. Thus, the following code creates identical rectangles r1 and r2 .

 Dim p As New Point(5, 5) Dim s As New Size(40, 20) Dim r1 As New Rectangle(p, s) Dim r2 As New Rectangle(5, 5, 40, 20) 

A RectangleF is a combination of a PointF and a SizeF . The properties and methods of RectangleF are entirely parallel to those of Rectangle , and we will focus on Rectangle in the rest of this section.

There are numerous properties that you can use in working with rectangles. Table 11-2 shows read/write properties.

Table 11-2. Read/Write Properties of Rectangle
Type Property Description
Point Location Location of top-left point of rectangle
Size Size Size of rectangle
Integer X x-coordinate of top-left point
Integer Y y-coordinate of top-left point
Integer Width Width of rectangle
Integer Height Height of rectangle

Table 11-3 shows read-only properties.

Table 11-3. Read-Only Properties of Rectangle
Type Property Description
Integer Left Same as X
Integer Top Same as Y
Integer Right X + Width
Integer Bottom Y + Height

There are a number of methods you can use in working with rectangles. Many of these methods have several overloaded versions. For example, you can offset a rectangle by either a point or a pair of integers. Table 11-4 shows several of the common methods of the Rectangle class.

Table 11-4. Common Methods of Rectangle
Method Description
Offset Shift a rectangle to another location
Inflate Make rectangle larger (or smaller with negative argument)
Union Form the union of two rectangles
Intersect Take the intersection of two rectangles
Contains Check if a point is contained within a rectangle
Using Rectangles in Drawing

It is legal for the Width and Height properties of a Rectangle to be negative, but when you use a rectangle for drawing, these values should be positive. The "top-left" point of a rectangle that you draw should really be in that location. If you have two arbitrary points p and q , you can use the helper function MakeRectangle to create a rectangle with top-left and bottom-right properly oriented for drawing. (This function was created for this book and is not part of the Framework.)

 graphics/codeexample.gif Private Function MakeRectangle(ByVal p As Point, _  ByVal q As Point) As Rectangle    Dim top, left, bottom, right As Integer    top = IIf(p.Y < q.Y, p.Y, q.Y)    left = IIf(p.X < q.X, p.X, q.X)    bottom = IIf(p.Y > q.Y, p.Y, q.Y)    right = IIf(p.X > q.X, p.X, q.X)    Return New Rectangle(left, top, right - left, _       bottom - top) End Function 

The program RectangleDemo\Version 1 illustrates drawing a rectangle from the point (20, 10) to the point (x, y), where the mouse is clicked. The drawing code is directly in the mouse event handler method.

 Private Sub Form1_MouseDown(ByVal sender As Object, _  ByVal e As MouseEventArgs) Handles MyBase.MouseDown    Dim g As Graphics = CreateGraphics()    Dim p1 As New Point(20, 10)    Dim p2 As New Point(e.X, e.Y)    Dim rect As Rectangle = MakeRectangle(p1, p2)    g.DrawRectangle(Pens.Black, rect) End Sub 

Color Structure

Colors in Windows Forms are defined by the three primary colors, red, green, and blue. The Color structure has corresponding read-only Byte properties R , G , and B . By varying the intensity of each component from 0 to 255, different colors can be created. Table 11-5 shows the RGB values of the three primary colors, the colors made up of exactly two primaries, white, a middle shade of gray, and black.

Table 11-5. RGB Values of Common Colors
Color RGB Value
Black 0, 0, 0
Red 255, 0, 0
Green 0, 255, 0
Blue 0, 0, 255
Yellow 255, 255, 0
Magenta 255, 0, 255
Cyan 0, 255, 255
Gray 127, 127, 127
White 255, 255, 255

There is also an alpha value, which specifies the transparency of the color. The corresponding read-only property is A , which is also of type Byte . A value of 0 means completely transparent, and a value of 255 means completely opaque .

A Color object can be created from the shared method FromArgb , which has several overloaded forms. For example, the following line of code creates a Color object m_color from the Integer variables a , r , g , and b .

 graphics/codeexample.gif m_color = Color.FromArgb(a, r, g, b) 
Standard Colors

The Color structure has 141 shared read-only properties that define a set of colors originally created for the Web. Thus, you can use names like Color.Black , Color.Red , and so on. There is also Color.Transparent .

Demonstration Program

The program ColorDemo\Version 1 displays the R, G, B, and alpha values of a color, which is initialized to green. The color can be changed by using the Color Common Dialog. The chosen color is shown in a label control. You can also assign numerical values for the R, G, B, and alpha values and click the Refresh button to display the chosen color. The background color of the form and the label control is set to white so that the color will be better displayed and not mix with the normal gray color of controls. Figure 11-3 shows a screen capture of the running program with the starting value of green.

Figure 11-3. Demonstration program shows R, G, B, and Alpha values.

graphics/11fig03.jpg

If you experiment with the alpha value, you will see how the color fades out with lower alpha values. However, the opaque quality is not demonstrated very well, because the text in the label control is in the foreground and is always shown. Version 2 of the program, discussed a little later, will use a brush to paint the label control, and we will then see that an opaque alpha value indeed covers up the text, and a transparent value lets the text show through.

Here is the code of the program.

 Public Class Form1    Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code "    Private m_color As Color    Private Sub Form1_Load(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles MyBase.Load       m_color = Color.FromArgb(255, 0, 255, 0)       ShowColor()    End Sub    Private Sub btnRefresh_Click(ByVal sender As Object, _     ByVal e As EventArgs) Handles btnRefresh.Click       Dim r As Integer = Convert.ToInt32(txtRed.Text)       Dim g As Integer = Convert.ToInt32(txtGreen.Text)       Dim b As Integer = Convert.ToInt32(txtBlue.Text)       Dim a As Integer = Convert.ToInt32(txtAlpha.Text)       m_color = Color.FromArgb(a, r, g, b)       lblColor.BackColor = m_color    End Sub    Private Sub btnColor_Click(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles btnColor.Click       Dim dlg As New ColorDialog()       Dim status As DialogResult       dlg.Color = m_color       status = dlg.ShowDialog       If status = DialogResult.OK Then          m_color = dlg.Color          ShowColor()       End If    End Sub    Private Sub ShowColor()       txtRed.Text = m_color.R       txtGreen.Text = m_color.G       txtBlue.Text = m_color.B       txtAlpha.Text = m_color.A       lblColor.BackColor = m_color    End Sub End Class 

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