GraphicsPath Objects


A GraphicsPath object represents a path defined by lines, curves, text, and other drawing commands. A GraphicsPath can even include other GraphicsPath objects.

You can use a Graphics object’s DrawPath and FillPath methods to draw or fill a GraphicsPath. For example, the following code creates a GraphicsPath object and adds a string to it. It creates a TextureBrush from a PictureBox image and uses the FillPath method to fill the path with the TextureBrush. It finishes by calling the DrawPath method to outline the path in black.

  Private Sub Form1_Paint(ByVal sender As Object, _  ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     ' Make a GraphicsPath containing text.     Dim txt As String = "Path"     Using graphics_path As New GraphicsPath         graphics_path.AddString(txt, _             New FontFamily("Times New Roman"), _             FontStyle.Bold, 150, _             New Point(0, 0), _             New StringFormat)         ' Fill the path with an image.         Using smiley_brush As New TextureBrush(picSmiley.Image)             e.Graphics.FillPath(smiley_brush, graphics_path)         End Using         e.Graphics.DrawPath(Pens.Black, graphics_path)     End Using End Sub 

Figure 21-13 shows the result.

image from book
Figure 21-13: A program can use a Graphics object’s FillPath and DrawPath methods to fill and draw a GraphicsPath object.

In addition to drawing and filling a GraphicsPath, you can also use one to define a region. The following code creates a GraphicsPath representing text much as the previous example does. It then sets the form’s Region property equal to a new Region object created from the GraphicsPath. This restricts the form to the region. Any pieces of the form that lie outside of the textual path are chopped off, so they are not drawn and mouse events in those areas fall through to whatever lies below the form.

When you use a path to define a form’s region, the path is taken relative to the form’s origin, which is not the same as the origin of the form’s client area. The form’s origin is at the upper-left corner of the form, including its borders and title bar. To allow for this difference in origins, the code uses the PointToScreen method to get the screen coordinates of the client area’s origin.

The code applies a translation transformation to the Graphics object so the client area origin is mapped to the form’s origin. It then sets the Graphics object’s SmoothingMode, fills the path with light gray, and then outlines the path with a thick black pen.

  Private Sub Form1_Paint(ByVal sender As Object, _  ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     ' Make a GraphicsPath containing text.     Dim txt As String = "Path"     Using graphics_path As New GraphicsPath         graphics_path.AddString(txt, _             New FontFamily("Times New Roman"), _             FontStyle.Bold, 150, _             New Point(0, 0), _             New StringFormat)         ' Set the form's region to the path.         Me.Region = New Region(graphics_path)         ' Fill the path with white and outline it in black.         Dim origin As Point = Me.PointToScreen(New Point(0, 0))         e.Graphics.TranslateTransform(Me.Left - origin.X, Me.Top - origin.Y)         e.Graphics.SmoothingMode = SmoothingMode.AntiAlias         e.Graphics.FillPath(Brushes.LightGray, graphics_path)         Using black_pen As New Pen(Color.Black, 5)              e.Graphics.DrawPath(black_pen, graphics_path)         End Using     End Using End Sub  

Figure 21-14 shows the result. Here the form is sitting above the Visual Basic development environment.

image from book
Figure 21-14: A program can use a GraphicsPath to define a form’s Region.

Because the 5-pixel-wide line around the path is centered on the edge of the form’s region, half of it is cut off.

Note also that the path used in this example cut the form’s borders and title bar off, so the user has no way to resize, move, or close this form. If you use this technique in an application, be sure to at least provide some method for the user to close the form. For example, you could provide a button or context menu. Or you could unload the form if the user clicked on it or pressed a certain key while the form had the input focus.

One more use for GraphicsPath objects is to define clipping regions. The following code creates a GraphicsPath containing text much as the previous examples do. It then calls the Graphics object’s SetClip method to make this path the form’s clipping region. Next, the program draws 200 lines between randomly generated points on the form. Only the parts of the lines inside the clipping region are drawn.

  Private Sub Form1_Paint(ByVal sender As Object, _  ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     ' Make a GraphicsPath containing text.     Dim txt As String = "Path"     Using graphics_path As New GraphicsPath         graphics_path.AddString(txt, _             New FontFamily("Times New Roman"), _             FontStyle.Bold, 150, _             New Point(0, 0), _             New StringFormat)         e.Graphics.SetClip(graphics_path)     End Using     ' Fill the ClientRectangle with white.     e.Graphics.FillRectangle(Brushes.White, Me.ClientRectangle)     ' Draw a bunch of random lines on the form.     Dim rnd As New Random     Dim x1, y1, x2, y2 As Integer     For i As Integer = 1 To 200         x1 = rnd.Next(0, Me.ClientSize.Width - 1)         y1 = rnd.Next(0, Me.ClientSize.Height - 1)         x2 = rnd.Next(0, Me.ClientSize.Width - 1)         y2 = rnd.Next(0, Me.ClientSize.Height - 1)         e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2)     Next i End Sub 

Figure 21-15 shows the result.

image from book
Figure 21-15: A program can use a GraphicsPath object to define a clipping region.

The GraphicsPath class provides many methods for adding lines, curves, text, and other shapes to the path. These methods include AddArc, AddBezier, AddBeziers, AddClosedCurve, AddCurve, AddEllipse, AddLine, AddLines, AddPath, AddPie, AddPolygon, AddRectangle, AddRectangles, and AddString. These are roughly analogous to the Draw and Fill methods provided by the Graphics object. For example, DrawEllipse draws an ellipse, FillEllispe fills an ellipse, and AddEllipse adds an ellipse to a path.

The following table describes the GraphicsPath object’s other most useful properties and methods.

Open table as spreadsheet

Property or Method

Purpose

CloseAllFigures

Closes all open figures by connecting their last points with their first points, and then starts a new figure.

CloseFigure

Closes the current figure by connecting its last point with its first point, and then starts a new figure. For example, if you draw a series of lines and arcs, this method closes the figure.

FillMode

Determines how the path handles overlaps when you fill it. This property can take the values Alternate and Winding. Figure 21-16 shows the difference.

Flatten

Converts any curves in the path into a sequence of lines. For example, this lets you explicitly calculate colors for every point in the path when setting a PathGradientBrush object’s SurroundColors property.

GetBounds

Returns a RectangleF structure representing the path’s bounding box.

GetLastPoint

Returns the last PointF structure in the PathPoints array.

IsOutlineVisible

Returns True if the indicated point lies beneath the path’s outline.

IsVisible

Returns True if the indicated point lies within the path’s interior.

PathData

Returns a PathData object that encapsulates the path’s graphical data. It holds arrays similar to those returned by the PathPoints and PathTypes properties.

PathPoints

Returns an array of PointF structures giving the points in the path.

PathTypes

Returns an array of Bytes representing the types of the points in the path.

PointCount

Returns the number of points in the path.

Reset

Clears the path data and resets FillMode to Alternate.

Reverse

Reverses the order of the path’s data.

StartFigure

Starts a new figure so future data is added to the new figure. Later, calling CloseFigure will close this figure, but not the previous one.

Transform

Applies a transformation matrix to the path.

Warp

Applies a warping transformation to the path. The transformation is defined by mapping a parallelogram to a rectangle.

Widen

Enlarges the curves in the path to enclose a line drawn by a specific pen.

image from book
Figure 21-16: The GraphicsPath object’s FillMode property determines how areas between the lines are filled.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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